[英]Socket.io not listening
im using this simple code 我正在使用这个简单的代码
Client.html Client.html
http://pastebin.com/c1C2F9MQ http://pastebin.com/c1C2F9MQ
Server.js Server.js
http://pastebin.com/Cqjhjfwm http://pastebin.com/Cqjhjfwm
Everything working fine till the last step of my client code 一切正常,直到我的客户代码的最后一步
socket.on('add', function(data) {
socket.broadcast.emit('AAA');
});
It seems that the socket add never comes but on my server I have 似乎套接字添加永远不会到来,但是在我的服务器上
socket.on('Text', function(data) {
socket.emit('add', data);
}); });
And I tested if socket text was coming and it does, I cant find the problem Thanks 而且我测试了套接字文本是否到来并且确实可以,我找不到问题,谢谢
socket.broadcast.emit
sends message to all sockets connected to server except the concerned socket
. socket.broadcast.emit
向所有连接到服务器的套接字发送消息,有关socket
除外。 So most likely add
does come to server but it broadcasts AAA
which your client cannot get. 因此,最有可能
add
到服务器,但它广播客户端无法获得的AAA
。 Use io.sockets.emit
to send to all connected sockets. 使用
io.sockets.emit
发送到所有连接的套接字。 Change this 改变这个
socket.broadcast.emit('AAA');
to 至
io.sockets.emit('AAA');
I too overlooked that you are calling socket.broadcast.emit
from client not from server. 我也忽略了您从客户端而不是从服务器调用
socket.broadcast.emit
。 It would have shown error on browser console, since broadcast is absent on client. 由于客户端上没有广播,因此在浏览器控制台上会显示错误。
Currently your on('add')
code on the client side is within the on('connect')
event which is not correct... 当前,您
on('add')
客户端的on('add')
代码位于on('connect')
事件中,这是不正确的...
You need to take it ouside there, so it becomes: 您需要将它带走,这样它就变成了:
socket.on('connect', function () {
$('#button').click(function() {
var addtext = $('#text').val();
socket.emit('Text', addtext);
});
});
socket.on('add', function(data) {
socket.emit('AAA');
});
EDIT : I also just noticed you had socket.broadcast.emit()
in your client side code. 编辑 :我也刚刚注意到您在客户端代码中有
socket.broadcast.emit()
。 As far as I know there's no concept of broadcasting from the client. 据我所知,没有从客户那里广播的概念。 If you want to broadcast something then the client should send it to the server then the server broadcasts to the other clients.
如果要广播某些内容,则客户端应将其发送到服务器,然后服务器将其广播到其他客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.