首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跨房间沟通之谜

跨房间沟通之谜
EN

Stack Overflow用户
提问于 2012-03-09 10:18:10
回答 1查看 217关注 0票数 2

我试着在房间里传递一些信息。我有:

server.js

代码语言:javascript
复制
var io = require('socket.io').listen(8081);

var chat = io
  .of('/chat')
  .on('connection', function (socket) {
    socket.on('chatTestEmit1', function(d) {
        console.log('>> chatTestEmit1 >> '+JSON.stringify(d));
        console.log(io.sockets.manager.rooms);
        socket.to('/news').emit('newsTestEmit1', d);
    });
  });

var news = io
  .of('/news')
  .on('connection', function (socket) {
    socket.on('checkAlive', function(d) {
        console.log('>> checkAlive >> '+JSON.stringify(d));
    });
    socket.on('newsTestEmit1', function(d){
        console.log('>> newsTestEmit1 >> '+JSON.stringify(d));
    });
  });

client.html

代码语言:javascript
复制
<script src="http://localhost:8081/socket.io/socket.io.js"></script>
<script>
  var chat = io.connect('http://localhost:8081/chat')
    , news = io.connect('http://localhost:8081/news');

  chat.on('connect', function () {
    chat.emit('chatTestEmit1',{msg:'This is the message from client.'});
  });

  news.on('connect', function(){
    news.emit('checkAlive',{msg:'News is alive'});
  });
</script>

日志如下所示:

代码语言:javascript
复制
   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 74858017692628057
   debug - setting request GET /socket.io/1/websocket/74858017692628057
   debug - set heartbeat interval for client 74858017692628057
   debug - client authorized for
   debug - websocket writing 1::
   debug - client authorized for /chat
   debug - websocket writing 1::/chat
   debug - client authorized for /news
   debug - websocket writing 1::/news
>> chatTestEmit1 >> {"msg":"This is the message from client."}
{ '': [ '74858017692628057' ],
  '/chat': [ '74858017692628057' ],
  '/news': [ '74858017692628057' ] }
   debug - websocket writing 5::/chat:{"name":"newsTestEmit1","args":[{"msg":"This is the message from client."}]}
>> checkAlive >> {"msg":"News is alive"}
   debug - emitting heartbeat for client 74858017692628057
   debug - websocket writing 2::
   debug - set heartbeat timeout for client 74858017692628057
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client 74858017692628057
   debug - set heartbeat interval for client 74858017692628057
   debug - emitting heartbeat for client 74858017692628057

如果此代码工作正常,它应该记录如下:

代码语言:javascript
复制
>> newsTestEmit1 >> {"msg":"This is the message from client."}

出现在日志里。请注意,我还尝试了:

代码语言:javascript
复制
io.sockets.in('/news').emit('newsTestEmit1', d);
io.sockets.in('news').emit('newsTestEmit1', d);
io.of('/news').emit('newsTestEmit1', d);

它们都不起作用。我错过了什么吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-03-12 05:16:04

我没有看过你所有的代码,但是一般的想法是,你想在发送消息时发出一个事件+来自客户端的数据,然后在服务器端设置一个事件,在发生事件时进行广播。下面是一些psuedo服务器端代码:

代码语言:javascript
复制
io.sockets.on('connection', function (socket) {
    console.log('Socket connection established.');
    socket.on('message event', function(data){
      socket.broadcast.emit('global chat update', data);}
      );
});

在客户端,您需要2位代码:

1)当聊天事件发生时,类似于:

代码语言:javascript
复制
socket.emit('message event', data)

2)当有全局聊天更新时显示最新聊天的内容。

代码语言:javascript
复制
socket.on('global chat update', function(data) { // your script to update chat data here };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9628072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档