TypeError:this.io未定义socket.io.js:639

时间:2014-10-13 20:26:00

标签: javascript node.js socket.io

我尝试从JavaScript连接到nodejs socket-server(在phonegap中使用webrtc - PhoneRTC插件)

我有server.js代码:

var app = require('express')();
var server = require('http').createServer(app);

var webRTC = require('webrtc.io').listen(server);
var io = require('socket.io')(server);
var port = process.env.PORT || 1234;
server.listen(port);




app.get('/', function(req, res) {
  res.sendfile(__dirname + '/index.html');
});

app.get('/style.css', function(req, res) {
  res.sendfile(__dirname + '/style.css');
});

app.get('/fullscrean.png', function(req, res) {
  res.sendfile(__dirname + '/fullscrean.png');
});

app.get('/script.js', function(req, res) {
  res.sendfile(__dirname + '/script.js');
});

app.get('/webrtc.io.js', function(req, res) {
  res.sendfile(__dirname + '/webrtc.io.js');
});





webRTC.rtc.on('chat_msg', function(data, socket) {
  var roomList = webRTC.rtc.rooms[data.room] || [];

  for (var i = 0; i < roomList.length; i++) {
    var socketId = roomList[i];

    if (socketId !== socket.id) {
      var soc = webRTC.rtc.getSocket(socketId);

      if (soc) {
        soc.send(JSON.stringify({
          "eventName": "receive_chat_msg",
          "data": {
            "messages": data.messages,
            "color": data.color
          }
        }), function(error) {
          if (error) {
            console.log(error);
          }
        });
      }
    }
  }
});

启动它:node server.js

然后我有客户端代码:

<script src="http://my-saite:1234/socket.io/socket.io.js"></script>



<script>
    // Create SocketIO instance, connect

    var socket = new io.Socket();

    socket.connect('http://my-saite:1234'); 

    // Add a connect listener
    socket.on('connect',function() {
      alert ('Client has connected to the server!');
    });
    // Add a connect listener
    socket.on('message',function(data) {
      alert ('Received a message from the server!',data);
    });
    // Add a disconnect listener
    socket.on('disconnect',function() {
      alert ('The client has disconnected!');
    });

    // Sends a message to the server via sockets
    function sendMessageToServer(message) {
      socket.send(message);
    };
</script>

当我在网络浏览器中打开它时我得到错误:TypeError:this.io未定义socket.io.js:639

http://my-saite.com:1234http://my-saite.com:1234/socket.io/socket.io.js加载正常,  socket.io.js中的639行是

/**
 * `Socket` constructor.
 *
 * @api public
 */

function Socket(io, nsp){
  this.io = io;
  this.nsp = nsp;
  this.json = this; // compat
  this.ids = 0;
  this.acks = {};
/*639 line --->*/  if (this.io.autoConnect) this.open();
  this.receiveBuffer = [];
  this.sendBuffer = [];
  this.connected = false;
  this.disconnected = true;
  this.subEvents();
}

如果有人遇到类似的问题 - 帮助。我很感激你的意见!

1 个答案:

答案 0 :(得分:1)

我想使用版本1.1.0?

更改Socket对象的创建

var socket = io.connect('http://my-saite:1234');

然后其余部分不应成为问题

// Add a connect listener
socket.on('connect',function() {
  alert ('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
  alert ('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
  alert ('The client has disconnected!');
});

// Sends a message to the server via sockets
function sendMessageToServer(message) {
  socket.send(message);
};