Socket.IO TypeError:无法读取未定义的属性“发射" [英] Socket.IO TypeError: Cannot read property 'emit' of undefined

查看:248
本文介绍了Socket.IO TypeError:无法读取未定义的属性“发射"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote the following code for chat client to client(client1 to server server to client2) but I am getting an error:

socket上缺少错误处理程序. TypeError:无法读取未定义的属性发射"

Missing error handler on socket. TypeError: Cannot read property 'emit' of undefined

这是我的服务器端代码.

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

和客户端代码

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

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

it complile with this command: node app.js : compiled [OK]

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

When I enter or login: Its work fine [OK]

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

When I send message to another client: failed [Not ok] It gave me following error:

用户ID为1

用户名是测试

test加入了该ID:1

test Joined with this Id: 1

用户ID为2

用户名是test2

test2与此ID结合在一起:2

test2 Joined with this Id: 2

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

{配方:'1',消息:'ssfsfs'}

{ recipent: '1', message: 'ssfsfs' }

接收者ID 1

是的用户退出

发送:ssfsfs

socket上缺少错误处理程序.

Missing error handler on socket.

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

TypeError: Cannot read property 'emit' of undefined

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

I do my best to explain my code am using socket.io lib new version

推荐答案

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

You've binded Your app to 3000-th port, and set socket.io to work with http module that listens on 3000.

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

So You've to change on clientside this:

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;

我在这里看不到任何逻辑:) 为什么不让它像这样:

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

这篇关于Socket.IO TypeError:无法读取未定义的属性“发射"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆