Node.js Socket.io - 如何向另一个侦听器中的套接字室发送消息?

时间:2017-01-11 20:12:30

标签: javascript node.js sockets socket.io

我试图向特定的房间发出消息,一旦#34;加入房间'监听器由客户端触发,如果我将代码放在joinedRoom监听器之外,代码工作正常,否则它什么都不做。

代码:

app.get('/room/:room/user/:user', function(req, res){
    var room = {
        username: req.params.user,
        roomname: req.params.room
    };
    res.render('room', room);
});

var users  = {};

io.sockets.on('connection', function (socket) {
    socket.on('joinedRoom', function(roomData){
        socket.username = roomData.username;
        socket.room = roomData.roomname;

        console.log("Roomname: " + socket.room);
        console.log("Username: " + socket.username);

        socket.join(socket.room);
        socket.broadcast.to(socket.room).emit('newUser', socket.username);

        socket.on('disconnect', function(){
            socket.broadcast.emit('userLeft', socket.username);
            socket.leave(socket.room);

            console.log('Connection id: ' + socket.id);
        });
    });
});

客户代码:

var socket, roomname, ioRoom;
var socket = io.connect('http://localhost:3000');

socket.on('enterRoom', function(roomname){
    console.log("ENTERED ROOM: " + roomname);
});

socket.on('newUser', function(username){
    pushUserName(username);
    pushUserStatus(username, ' has joined the room <br/>')
});

socket.on('newRoom', function(data){
    alert(data)
});

socket.on('userLeft', function(username){
    pushUserStatus(username, ' has left the room <br/>')
})

function pushUserName(username){
    var el = document.getElementById("username");
    el.innerHTML += username + '<br/>';
}

function pushUserStatus(username, message){
    var el = document.getElementById("joined");
    el.innerHTML += username + message;
}

代码发出&#34; joinedRoom&#34;事件:

doctype html
head
    title Sala #{roomname}

    script(src="https://cdn.socket.io/socket.io-1.4.5.js")
    script(type="text/javascript").
        var socket = io.connect('http://localhost:3000');
        var roomData = {
            roomname: '#{roomname}',
            username: '#{username}'
        }
        socket.emit('joinedRoom', roomData);

    script(src="../../../js/room.js")

  body
      p#joined
      h1 Room's name: #{roomname}

      h2 Your nickname: #{username}

      h2 Players:

      p#username.player-row

我看到了文档和一些示例代码,一切似乎都是正确的(当它只是语法时)我在这里错过了一些简单的东西吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您需要在连接事件侦听器之外注册disconnect事件侦听器。

代码:

app.get('/room/:room/user/:user', function(req, res){
    var room = {
        username: req.params.user,
        roomname: req.params.room
    };
    res.render('room', room);
});

var users  = {};

io.sockets.on('connection', function (socket) {
    socket.on('joinedRoom', function(roomData){
        socket.username = roomData.username;
        socket.room = roomData.roomname;

        console.log("Roomname: " + socket.room);
        console.log("Username: " + socket.username);

        socket.join(socket.room);
        socket.broadcast.to(socket.room).emit('newUser', socket.username);
    });
    socket.on('disconnect', function(){
        // need more logic here
    });
});

答案 1 :(得分:0)

对于那些可能有类似问题的人来说,代码大多是正确的,但我错过了这个:

而不是:

socket.emit('joinedRoom', roomData);

应该是:

socket.on('connect', function(){
    socket.emit('joinedRoom', roomData);
})

:)