'this' in websocket joinRoom controller not set to class
When trying to join a room using websockets, 'this' in the joinRoom function does not seem to be bound to the class. In order to test this I installed a fresh version of adonis and changed the following files:
socket.js
Append: Ws.channel('chat', 'ChatController')
Created ChatController in Ws/Controllers and add:
'use strict'
class ChatController {
constructor (socket, request) {
this.socket = socket
this.request = request
}
* joinRoom (channel) {
console.log('trying to join room', this.socket)
}
* onMessage (message) {
console.log('message', this.socket)
}
}
module.exports = ChatController
In a view I return:
<script src="https://unpkg.com/adonis-websocket-client/dist/ws.min.js"></script>
<script>
const io = ws('http://localhost:3333', {})
const client = io.channel('chat')
client.connect(function (error, connected) {
if (error) {
// do something
console.log(error)
}
client.emit('message', 'Hello world')
client.joinRoom('lobby', {}, function (error, joined) {
// status
})
})
</script>
When I go to the page in my browser, I get the following message in the console output:
trying to join room undefined
Is this expected behaviour? Docs here show that I should be able to access the socket.
The onMessage function is able to access this.socket as expected.
Try this:
* joinRoom (room, body, socket) {
// socket
}
* leaveRoom (room, body, socket) {
// socket
}
Any news @Nublust ?
Sorry for the delay @RomainLanz, only have the weekends to work on this :).
@K-Eo's suggestion did indeed work. Is it the docs that are wrong, or do you think there is a bug?
I have the exact same problem. Amazing framework btw
Should be fixed in new version of websockets. Closing since this is wrong repo and websocket related issues are tracked separately on https://github.com/adonisjs/adonis-websocket repo
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.