簡體   English   中英

Socket.IO未定義變量

[英]Socket.IO undefiend variable

我正在使用socket.io創建一個交互式圖形應用程序。 在服務器端有一個graph變量。 當用戶加載頁面時,測試事件將發送到服務器,服務器將設置變量並將其返回給用戶。 我有第二個事件用於節點拖動,但是當我嘗試拖動節點時,服務器說該圖的節點和鏈接變量未定義。

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);
  });
});

通過替換以下行解決了該問題:

io.on('connect', function(socket){

有了這個:

io.sockets.on('connect', function(socket){

不知道為什么它起作用,但是它起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM