Socket.io客户端事件触发
我要当client.htmlSocket.io客户端事件触发
两只苍蝇的部分提示我server.js执行特定的功能如下:
Server.js:
io.sockets.on('connection', function (socket) {
socket.on('key',function(data){
console.log('done'); //this statement is never executed
});
});
客户:
<script>
var socket = io.connect();
socket.on('connect', function() {
socket.emit('key',{
string:'string'
});
</script>
的console.log( '完成');永远不会执行..不知道为什么...请帮助我们.. :)
您是否包含socket
js文件?
<script src="/socket.io/socket.io.js"></script>
是的,我已经包含了js文件@ashley – 2013-03-07 14:35:01
刚刚实现,在'var socket = io。 connect();''你需要指定url:'var socket = io.connect('http:// localhost:3000');' – ashley 2013-03-07 14:36:05
仍然不工作@ ashley..i已经试过 – 2013-03-07 14:45:17
老问题,但您的代码不工作的原因是因为你缺少你收:});
在你的客户端代码。
解决方案:
<script>
var socket = io.connect();
socket.on('connect', function() {
socket.emit('key',{
string:'string'
});
}); //<--- Missing closing tag
</script>
您是否获得在浏览器控制台的任何错误? – 2013-03-08 03:35:24
不... @Jayantha – 2013-03-11 16:34:38
你有没有听过任何教程? http://codetuner.blogspot.com/2012/04/real-time-web-sample-using-socketio-and.html – 2013-03-12 03:24:36