简体   繁体   English

在socket.io中从客户端发送广播消息

[英]sending broadcast message from the client side in socket.io

In the socket.io client socket section of the doc http://socket.io/docs/client-api/#io#socket refers to Socket docs, which implies both server Socket object and client Socket object are the same. 在文档http://socket.io/docs/client-api/#io#socket的socket.io客户端套接字部分中引用了Socket文档,这意味着服务器Socket对象和客户端Socket对象是相同的。

Also the docs http://socket.io/docs/rooms-and-namespaces/#default-room here has the following code which has a broadcast object attached to socket object. 此外,文档http://socket.io/docs/rooms-and-namespaces/#default-room在此处具有以下代码,该代码具有附加到套接字对象的广播对象。 ie. 即。 socket.broadcast. socket.broadcast。

io.on('connection', function(socket){
  socket.on('say to someone', function(id, msg){
    socket.broadcast.to(id).emit('my message', msg);
  });
});

However the socket.broadcast property is not accessible on the client side. 但是,无法在客户端访问socket.broadcast属性。 It throws Type Error. 它会抛出类型错误。 Does this mean that broadcasting can not be done from client side . 这是否意味着无法从客户端进行广播

Yes, broadcasting cannot be done from the client side. 是的,无法从客户端进行广播。 This is because they may both be sockets but they are not considered the same. 这是因为它们可能都是套接字但它们不被认为是相同的。 Check out these two: 看看这两个:

Socket.io-client and Socket.io Socket.io-clientSocket.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM