简体   繁体   English

在将 node.js 用于 socket.io 时遇到问题

[英]having problem in in using node.js for a socket.io

io.on('connection', socket =>{ ^ io.on('连接', socket =>{ ^

TypeError: io.on is not a function at Object. (D:\shalini course\projects\chat app\nodeserver\nodeapp.js:7:4) at Module._compile (node:internal/modules/cjs/loader:1218:14) at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) at Module.load (node:internal/modules/cjs/loader:1081:32) at Module._load (node:internal/modules/cjs/loader:922:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 TypeError: io.on 不是 function at Object. (D:\shalini course\projects\chat app\nodeserver\nodeapp.js:7:4) at Module._compile (node:internal/modules/cjs/loader:1218 :14) 在 Module._extensions..js (node:internal/modules/cjs/loader:1272:10) 在 Module.load (node:internal/modules/cjs/loader:1081:32) 在 Module._load (node :internal/modules/cjs/loader:922:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47

it shows this error when I run a cmd - node nodeapp.js please do reply If any idea how to fix it?当我运行 cmd - node nodeapp.js 时显示此错误,请回复如果有任何想法如何解决它?

enter image description here I tried running an application of socket.io on 5000 port using node.js but it's not working. enter image description here我尝试使用 node.js 在 5000 端口上运行 socket.io 的应用程序,但它不起作用。

you have to add the socket.io server to the http server, the mistake you are doing is that you are adding the socket.io server to nowhere, to the aire, try with this before to iniltialize the server on port 5000你必须将 socket.io 服务器添加到 http 服务器,你做的错误是你将 socket.io 服务器添加到无处,到 aire,之前尝试在端口 5000 上初始化服务器

//CREATE THE SERVER NODE JS
const server = http.createServer(app)


// CREATE SOCKET.IO SERVER CONNECT WITH OUR SERVER
const io = new Server(server);


HERE PUT YOUR SOCKET IO CODE, io.on,.....

server.listen(port,()=>{
    console.log(`listening on port ${port}`)
});


  • Yes, "io" that you are trying to use a method on is not a function on that class in your code "io" includes the whole package of socketio all the classes and functions是的,您尝试使用方法的“io”不是代码“io”中的那个 class 上的 function 包括 socketio 的整个 package 所有类和函数
  • "io" in your code has all the classes that your can use by您代码中的“io”包含您可以使用的所有类


    const myClass = new io.(someclass)()

  • You can impove your code like this你可以像这样改进你的代码


    const socketiopackage = require('socket.io')
    const port = 5000
    const io = new socketiopackage.Server(5000,{cors: {"Access-Control-Allow-Origin": "*",methods: ["GET", "POST", "OPTIONS"]},})
    var users = {}
    
    io.on('connection',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,name:user[socket.id]})
        })
    })

  • or it is easier to directly import your required class或者更容易直接导入您需要的 class


    const { Server } = require('socket.io')
    const port = 5000
    const io = new Server(5000,{cors: {"Access-Control-Allow-Origin": "*",methods: ["GET", "POST", "OPTIONS"]},})
    var users = {}
    
    io.on('connection',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,name:user[socket.id]})
        })
    })

  • I have added some cors settings in the options for creating a server, it is a good practice to do so我在创建服务器的选项中添加了一些 cors 设置,这是一个很好的做法

Using Nodemon使用 Nodemon

  • install it using安装它使用
npm install -g nodemon
  • then use it using然后使用它
nodemon (fileName).js

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

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