A
A
Artyom Prosvetov2017-02-07 17:32:51
Node.js
Artyom Prosvetov, 2017-02-07 17:32:51

How to get socket in joinRoom (adonis.js) method?

I need to get the user's socket in the joinRoom method. I have already tried everything: I removed the generators, and launched the constructor in the method (nonsense, but I decided to try it), I already took the code from the documentation, but it still does not work. Guys who worked with this? How can I get at least a socket, preferably all properties defined in the constructor.
Code App/Ws/Controllers/ChatController.js

'use strict'

class ChatController {
  constructor (socket) {
    this.socket = socket
  }

  * leaveRoom (room) {
    // Do cleanup if required
  }

  * joinRoom (room) {
    this.room = room
    this.socket.emit('server:message', {message: 'New connection!'})
  }
}

module.exports = ChatController

Code App/Ws/Controllers/ChatController.js
'use strict'

const Ws = use('Ws')

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Prosvetov, 2017-02-07
@Dragomeat

Found the answer by myself

* joinRoom (room, payload, socket) {     
      socket.emit('server:message', {message: 'New connection!'})
  }

E
emp1re, 2017-02-07
@emp1re

var chat = io.of('/chat') .on('connection', roomConnect); // nameSpace

function roomConnect(socket){  // heandler
 socket.join('room', function(){
 chat.to('room').emit('message', 'somemessage'); // message to room
}) // join in room

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question