简体   繁体   English

参考错误:未定义套接字.....使用 Socket.io 创建节点服务器时

[英]ReferenceError: Socket is not defined .....while creating a NODE SERVER using Socket.io

I was creating a Chat Application using Node JS where i have been using Socket.Io library to build a two way connection between the client and the web server.我正在使用 Node JS 创建一个聊天应用程序,我一直在使用 Socket.Io 库在客户端和 web 服务器之间建立双向连接。

Here is my code (Creating Node Server):这是我的代码(创建节点服务器):

// Node Server that will handle Socket io connections

const io = require('socket.io')(3000)            // Requiring the socket io Library

const users = {};

io.on("connections", (socket) => {
    socket.on("new-user-joined", (name) => {
        users[socket.id] = name;    
        socket.broadcast.emit("user-joined", name)
    });
});

socket.on('send', message =>{
    socket.broadcast.emit('recieve', {message: message, name: users[socket.id]});
});

My node server stop and display this error:我的节点服务器停止并显示此错误:

Problem:问题:

    PS C:\Users\SANKET PRAKHER\Desktop\complete web development bootcamp\Chat Application\Node Server> nodemon .\index.js
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node .\index.js`
C:\Users\SANKET PRAKHER\Desktop\complete web development bootcamp\Chat Application\Node Server\index.js:14
socket.on('send', message =>{
^

ReferenceError: socket is not defined
    at Object.<anonymous> (C:\Users\SANKET PRAKHER\Desktop\complete web development bootcamp\Chat Application\Node Server\index.js:14:1)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

Your reference to variable socket where you wrote socket.on('send', ... is simply out of scope where the socket itself is a defined variable. This variable only defined in the function that you give as second argument to the io.on call and should be referenced there normally. And that function takes it as an argument named socket by the way.您对编写socket.on('send', ...的变量套接字的引用完全来自 scope ,其中socket本身是一个已定义的变量。此变量仅在您作为第二个参数提供给 ZF98ED07A4D95F514F7DEF4 的io.on ,应该在那里正常引用。顺便说一句,function 将它作为一个名为socket的参数。

So you should carry your socket.on code block inside that function:所以你应该在 function 中携带你的socket.on代码块:

io.on("connections", (socket) => {
    socket.on("new-user-joined", (name) => {
        users[socket.id] = name;    
        socket.broadcast.emit("user-joined", name)
    });
    socket.on('send', message => {
        socket.broadcast.emit('recieve', {message: message, name: users[socket.id]});
    });
});

just like you do for the socket.on('new-user-joined', ...)就像你对socket.on('new-user-joined', ...)

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

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