简体   繁体   English

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

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

I wrote the following code for chat client to client(client1 to server server to client2) but I am getting an error: 我为聊天客户端到客户端(客户端1到服务器服务器到client2)编写了以下代码,但出现错误:

Missing error handler on socket . socket上缺少错误处理程序。 TypeError: Cannot read property 'emit' of undefined TypeError:无法读取未定义的属性“ emit”

Here is my serverside code. 这是我的服务器端代码。

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

And client side code 和客户端代码

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

it complile with this command: node app.js : compiled [OK] 它符合以下命令:node app.js:已编译[确定]

When I enter or login: Its work fine [OK] 当我输入或登录时:工作正常[确定]

When I send message to another client: failed [Not ok] It gave me following error: 当我向另一个客户端发送消息时:失败[不正常]它给了我以下错误:

User Id is 1 用户ID为1

Username is test 用户名是test

test Joined with this Id: 1 测试与此ID连接:1

User Id is 2 用户ID为2

Username is test2 用户名是test2

test2 Joined with this Id: 2 test2加入了这个ID:2

here works fine and from here i tried to send message to user2/client2 that give me error 在这里工作正常,从这里我尝试将消息发送到user2 / client2,这给了我错误

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

reciver id 1 收款人编号1

yes user exits 是的,用户退出

Sending : ssfsfs 正在发送:ssfsfs

Missing error handler on socket . socket上缺少错误处理程序。

TypeError: Cannot read property 'emit' of undefined TypeError:无法读取未定义的属性“ emit”

I do my best to explain my code am using socket.io lib new version 我尽力解释我的代码正在使用socket.io lib新版本

You've binded Your app to 3000-th port, and set socket.io to work with http module that listens on 3000. 您已将您的应用程序绑定到第3000个端口,并将socket.io设置为与监听3000的http模块一起使用。

So You've to change on clientside this: 因此,您必须在客户端进行以下更改:

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

to this: 对此:

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

or just: 要不就:

var socket = io.connect();



also from Your code: 同样从您的代码:

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

I don't see any logic here :) Why not to keep it like: 我在这里看不到任何逻辑:)为什么不这样保留:

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.

相关问题 Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined - Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined Socket.io:“无法读取未定义的属性&#39;emit&#39;” - Socket.io : “Cannot read property 'emit' of undefined” AngularJS Socket.io:无法读取未定义的属性“ emit” - AngularJS Socket.io: Cannot read property 'emit' of undefined Socket.io 未捕获类型错误:无法读取未定义的属性“应用” - Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined Socket.io TypeError:无法读取未定义的属性“hteam” - Socket.io TypeError: Cannot read property 'hteam' of undefined Socket.io引发“ TypeError:无法读取未定义的属性&#39;push&#39;” - Socket.io raises “TypeError: Cannot read property 'push' of undefined” WebRTC,socket.io,node.js:无法读取未定义的属性&#39;emit&#39;? - WebRTC , socket.io, node.js: Cannot read property 'emit' of undefined? 使用React / Socket.IO无法读取未定义错误的属性&#39;emit&#39; - Cannot read property 'emit' of undefined error using React/Socket.IO 当socket.io发出“登录”消息时,TypeError:无法读取未定义的属性“ navigate” - When socket.io emits to 'login', TypeError: Cannot read property 'navigate' of undefined Karma无法读取未定义的socket.io的属性&#39;prototype&#39;? - Karma Cannot read property 'prototype' of undefined socket.io?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM