我正在尝试在同一页面(客户端)上有多个套接字io连接。我试过这个答案,但不幸的是它没有解决问题,Link to SO question
案例:
目前我的代码:
1. index.html上的声明脚本,(在localhost:3000上)
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="js/main.js"></script>
2。 main.js上的Socket.io逻辑(在localhost:3000上)
var socket = io('http://localhost:3000', {resource: '/socket.io', forceNew : true});
var cms_socket = io('http://localhost:8000', {resource: '/socket.io', forceNew : true});
socket.on('connect', console.log("SOCKET IO chat connected")) //WORKED
cms_socket.on('connect', console.log("SOCKET IO cms_connected")); //WORKED
socket.on('bot-reply', (data) => alert(data)) //WORKED
cms_socket.on('test_cms_socket', (data) => alert(data)) //NOT-WORKED
令我困惑的是,来自两个套接字的控制台日志工作,告诉两个套接字连接,尽管cms_socket.on('test_cms_socket', ..)
不起作用。
以下是CMS localhost:8000
的逻辑function broadcast(req,res){
res.io.emit('test_cms_socket', "This is from CMS");
return res.redirect('/');
}
我已将socket.io传递给中间件,使用了here
中的教程我真的不知道这是否有帮助,但我从控制台收到此错误。任何帮助将非常感谢
Uncaught TypeError: Cannot read property 'apply' of undefined
at r.emit (index.js:133)
at r.emit (socket.js:136)
at r.onconnect (socket.js:327)
at r.onpacket (socket.js:224)
at r.<anonymous> (index.js:21)
at r.emit (index.js:133)
at r.ondecoded (manager.js:345)
at s.<anonymous> (index.js:21)
at s.r.emit (index.js:133)
at s.add (index.js:241)