Using socket.io from a module

后端 未结 2 374
故里飘歌
故里飘歌 2021-01-12 23:36

My sio = require(\'socket.io\').listen(app) is in my server.js file, but I\'m calling a method in a library that would like to push a message to the client... s

2条回答
  •  悲&欢浪女
    2021-01-13 00:07

    What I understood from the question is you want to know how to use socketIO with node module.Based on my understanding you can use it as below: First install socketIO module locally with npm by running " $npm install socket.io " command for windows.

    Add Script to your HTML page:

    
    

    Now add var io = require('socket.io'); to your server or js file where you are going to use it.

    Then you can have server startup code listen to that server and on connection of it perform the options for any event.

    var listener = io.listen(server);
    listener.sockets.on('connection', function(socket) {
        socket.on('locationClick', function(data) {
            // perform the function on receving locationClick event.
        }
    }
    

提交回复
热议问题