[英]Socket.IO undefiend variable
I am using socket.io to create an interactive graph app. 我正在使用socket.io创建一个交互式图形应用程序。 on the server side have a graph variable. 在服务器端有一个graph变量。 When the user loads the page a test event is sent to the server, which sets up the variable and returns it to the users. 当用户加载页面时,测试事件将发送到服务器,服务器将设置变量并将其返回给用户。 I have a second event for node dragging, but when i try to drag the node the server says that the graph's nodes and link variables are undefined. 我有第二个事件用于节点拖动,但是当我尝试拖动节点时,服务器说该图的节点和链接变量未定义。
var express = require('express'),
app = express(),
http = require('http'),
socketIo = require('socket.io');
var server = http.createServer(app),
io = socketIo.listen(server);
var graph = {nodes : [], links : []}
server.listen(8080, function(){
console.log('server is listening on localhost:8080');
});
app.use(express.static(__dirname + '/'));
io.on('connect', function(socket){
// dump some test data
socket.on('test', function(){
// this just creates a few nodes in an array
var data = { ... }
graph = data;
io.emit('test', graph);
});
socket.on('eventNodeDragStart', function(index){
console.log('event node drag start: ' + index);
// undefiend, the graph.nodes is empty here
console.log(graph.nodes[index] + "\n\n");
// cannot read property of undefined
// also missing error handler on 'socket'
if(graph.nodes[index].locked) return;
graph.nodes[index].locked = true;
io.emit('eventNodeDragStart', index);
});
});
Solved the problem by replacing this line: 通过替换以下行解决了该问题:
io.on('connect', function(socket){
with this: 有了这个:
io.sockets.on('connect', function(socket){
Not sure why it works, but it does. 不知道为什么它起作用,但是它起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.