繁体   English   中英

Socket.IO TypeError:无法读取未定义的属性“发出”

[英]Socket.IO TypeError: Cannot read property 'emit' of undefined

我为聊天客户端到客户端(客户端1到服务器服务器到client2)编写了以下代码,但出现错误:

socket上缺少错误处理程序。 TypeError:无法读取未定义的属性“ emit”

这是我的服务器端代码。

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(3000);

var users = {};
var reciverusers = {};

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}
io.sockets.on('connection', function (socket) {
  socket.on("users",function(data){

    users[data.uid]=data.uid;
    users[data.uname] =data.uname;
    //console.log(data);
    console.log("User Id is "+users[data.uid]);
    console.log("Username is "+users[data.uname]);
    console.log(users[data.uname] + " Joined with this Id: " +users[data.uid]); 
    //console.log("cnsle users : "+users[0]);
  });

  socket.on("sendmsg", function(msgdata){
    console.log(msgdata);
    reciverusers[msgdata.recipent]=msgdata.recipent;
    reciverusers[msgdata.message]=msgdata.message;
    console.log("reciver id "+reciverusers[msgdata.recipent]);
    for(var name in users) {
        if(users[name] === reciverusers[msgdata.recipent]) {
            console.log("yes user exits");
            console.log("Sending : "+ reciverusers[msgdata.message]);
            io.sockets.emit("rmsg",reciverusers[msgdata.message]);
            io.sockets.connected[reciverusers[msgdata.recipent]].emit("rmsg", {'msg':reciverusers[msgdata.message]});
            break;
        }else{
            console.log("No user not exists");
        }
    }
  });

和客户端代码

var username,uid;
 var socket = io.connect('http://localhost');

 $(".client_name").on("submit", function(){
    // Tell the server about it
    var username = $("#cleintname").val();
    var userid = $("#cliendID").val();

    socket.emit("users", {"uname": username,"uid":userid});

    $(".client_name").remove();
    $("#Clientnm").text(username);
    return false;

  });

 var chat_form = $(".chatform");
 chat_form.on("submit", function(){
   // Send the message to the server
   var reciver_id = $("#reciver_id").val();
   var msg = $("#message").val();
   socket.emit("sendmsg", {"recipent":reciver_id, "message":msg});

   // Empty the form
  $("#message").val('');
  return false;
 });

 // Whenever we receieve a message, append it to the <ul>
 socket.on("rmsg", function(data){
  $("#messages").append(data);
 });

它符合以下命令:node app.js:已编译[确定]

当我输入或登录时:工作正常[确定]

当我向另一个客户端发送消息时:失败[不正常]它给了我以下错误:

用户ID为1

用户名是test

测试与此ID连接:1

用户ID为2

用户名是test2

test2加入了这个ID:2

在这里工作正常,从这里我尝试将消息发送到user2 / client2,这给了我错误

{配方:“ 1”,消息:“ ssfsfs”}

收款人编号1

是的,用户退出

正在发送:ssfsfs

socket上缺少错误处理程序。

TypeError:无法读取未定义的属性“ emit”

我尽力解释我的代码正在使用socket.io lib新版本

您已将您的应用程序绑定到第3000个端口,并将socket.io设置为与监听3000的http模块一起使用。

因此,您必须在客户端进行以下更改:

var socket = io.connect('http://localhost');

对此:

var socket = io.connect('http://localhost:3000');

要不就:

var socket = io.connect();



同样从您的代码:

users[data.uid]=data.uid;
users[data.uname] =data.uname;

我在这里看不到任何逻辑:)为什么不这样保留:

users[data.uid] = {uid: data.uid, uname: data.uname}
sockets[socket.id] = socket;
if(!users[data.uid].sockets) {
  users[data.uid].sockets = [];
}
users[data.uid].sockets.push(socket);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM