3

I'm trying to combine adonisjs(v4.1) websocket with gdscript.
And that means that I need to connect to adonisjs from godot by gdscript code.
Now the more clear question is "because adonisjs socket is running on a channel, how to subscribe to the adonisjs websocket channel?"
For more easier understanding the problem, there are some code that in the server side I created to communicate with gdscript and here they are:

socket.js

'use strict'

const Ws = use('Ws')

Ws.channel('chat', 'ChatController')

ChatController.js

'use strict'

class ChatController {
  constructor ({ socket, request }) {
    this.socket = socket
    this.request = request
    console.log('connected')
  }
  onMessage (message) {
    console.log(message)
  }
}

module.exports = ChatController

And here is the gdscript side code that successfully connects with a simple nodejs socket server with no problem but in channel structure that is implemented inside adonisjs, the connection will not happen.

socket.gd

extends Node2D

export var SOCKET_URL = "ws://127.0.0.1:3333"
var _client = WebSocketClient.new()


func _ready():
    _client.connect("connection_closed", self, "_on_connection_closed")
    _client.connect("connection_error", self, "_on_connection_closed")
    _client.connect("connection_established", self, "_on_connected")
    _client.connect("data_received", self, "_on_data")
    
    var err = _client.connect_to_url(SOCKET_URL)
    if err != OK:
        print('unable to connect')
        set_process(false)

func _process(delta):
    _client.poll()

func _on_connection_closed(was_clean = false):
    print('Closed, clean: %s' % was_clean)
    set_process(false)

func _on_connected(proto = ''):
    print('connected with protocol: %s' % proto)

func _on_data():
    var payload = JSON.parse(_client.get_peer(1).get_packet().get_string_from_utf8()).result
    print('received data: %s' % payload)

func _send():
    _client.get_peer(1).put_packet(JSON.print({"test": "Test"}).to_utf8())

Now how should I change the code or what should I add to socket.gd file to have a successful connection to adonisjs server and subscribe to chat channel that is defined in socket.js file?

Mahmood
  • 403
  • 2
  • 10

0 Answers0