因此,我有一个节点服务器,运行expressjs io(使用socket.io),并且我正在构建一个跟踪数据库中坐标的网格地图。
只是,我遇到了一个特殊的问题,因为我的套接字只监听有时.
起初没有错误消息,只是偶然我让页面运行并收到此错误。
Uncaught TypeError: Cannot call method '0' of undefined UkPS99A_w96Ae0K570Nt?t=1395276358213&i=0:1
当我点击文件时UkPS99A_w96Ae0K570Nt?t=1395276358213&i=0:1
我得到这个代码:
io.j[0]("8::");
如果我刷新页面,每隔几次它就会突然工作查找大约 10 次图块点击,然后停止工作。我的数据库正在正确更新,直到套接字基本上消失。
这是我在地图中发送坐标的位置:
io.emit("move", {x:this.x,y:this.y});
服务器监听:
app.io.route('move', function(req) {
con.getConnection(function(err){
if (err) console.log("Get Connection Error.. "+err);
//removed query because redundant
req.io.emit("talk", {x:req.data.x,y:req.data.y});
});
});
和我的套接字脚本:
io.on("talk",function(data) {
console.log(data.x,data.y);
});
我的脚本包含在页面底部,按以下顺序:
<script src="socket.io/socket.io.js"></script>
<script>io = io.connect();</script> <!-- open the socket so the other scripts can use it -->
<script src="../js/sock.js"></script>
<script src="../js/map.js"></script>
我做错了什么导致套接字似乎失去连接并抛出某种错误吗?
更新:我让服务器运行更长时间,并且控制台中弹出了更多错误消息:
Uncaught TypeError: Cannot call method 'close' of null socket.io.js:1967
Uncaught TypeError: Cannot call method 'close' of null socket.io.js:1967
Uncaught TypeError: Cannot call method 'onClose' of null
更多更新:更改连接线并将正确的 CORS 添加到我的 server.js
io = io.connect('http://sourceundead.com', {resource : 'socket.io'});
还是同样的问题。