最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rooms in socket.io. How to send messages? - Stack Overflow

programmeradmin1浏览0评论

I am new at node.js and socket.io, following the 'get started' code from official socket.io website, I wrote simple chat. Then I wanna create different rooms, like in messengers. But relying on official documentation, I can't do this as myself... Here is my server code:

io.on('connection', function(socket) {

    var room_name = socket.request.headers.referer; // link of the page, where user connected to socket

    // connecting to room
    socket.join(room_name, function(){
        //trying to send messages to the current room
        io.to(room_name, function () {
            socket.on('chat message', function (msg) {
                io.emit('chat message', msg);
            });
        });
    });

});

And my client side code:

        $(function () {
            var socket = io();
            //when we submit our form
            $('form').submit(function(){
                //we are sending data to socket.io on server
                socket.emit('chat message', $('#m').val());
                $('#m').val('');
                return false;
            });
            //And then we are creating message in our html code
            socket.on('chat message', function(msg){
                $('#messages').append($('<li>').text(msg));
            });
        });

Why this code ain't working? What sould I do, for sending messages to different rooms?

I am new at node.js and socket.io, following the 'get started' code from official socket.io website, I wrote simple chat. Then I wanna create different rooms, like in messengers. But relying on official documentation, I can't do this as myself... Here is my server code:

io.on('connection', function(socket) {

    var room_name = socket.request.headers.referer; // link of the page, where user connected to socket

    // connecting to room
    socket.join(room_name, function(){
        //trying to send messages to the current room
        io.to(room_name, function () {
            socket.on('chat message', function (msg) {
                io.emit('chat message', msg);
            });
        });
    });

});

And my client side code:

        $(function () {
            var socket = io();
            //when we submit our form
            $('form').submit(function(){
                //we are sending data to socket.io on server
                socket.emit('chat message', $('#m').val());
                $('#m').val('');
                return false;
            });
            //And then we are creating message in our html code
            socket.on('chat message', function(msg){
                $('#messages').append($('<li>').text(msg));
            });
        });

Why this code ain't working? What sould I do, for sending messages to different rooms?

Share Improve this question edited Mar 15, 2017 at 23:51 gyre 16.8k4 gold badges40 silver badges47 bronze badges asked Mar 15, 2017 at 23:38 Bim BamBim Bam 4392 gold badges6 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Add this to server code:

 var room_name = socket.request.headers.referer; // link of page, where user connected to socket

     //connecting to room
 socket.join(room_name);
 socket.on('chat message', function (msg) {
     io.to(room_name).emit('chat message', msg);
 });
发布评论

评论列表(0)

  1. 暂无评论