类型错误:this.io是未定义的套接字.io.js:639

TypeError: this.io is undefined socket.io.js:639

本文关键字:io 套接字 js 错误 this 类型 未定义      更新时间:2023-09-26

我尝试从JavaScript连接到nodejs套接字服务器(用于在phonegap-PhoneRTC插件中使用webrtc)

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

启动它:nodeserver.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>

当我在web浏览器中打开它时,我会得到错误:TypeError:this.io是未定义的socket.io.js:639

http://my-saite.com:1234和http://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.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);
};