Node server, Socket.io 'io is not defined'?

前端 未结 6 1581
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 06:25

I\'ve followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder.

6条回答
  •  一个人的身影
    2020-12-30 07:00

    Node.js newbie here! I am pretty sure this has been answered. Yet I kept finding problems with the src for the socket. Apparently this :

    (Edit: although this is obvious, this link may not work depending on when you are reading this answer. Please pick the latest link from: https://cdnjs.com/libraries/socket.io)

    Here is a working client side code:

    
    
    
    
    
    
    

    On the server side (handles only 1 socket)

    var app  = require('express')();
    var http = require('http').Server(app);
    var io   = require('socket.io')(http);
    var port = process.env.PORT || 8080;
    
    
    app.get('/', function(req, res){
        console.log("app works");
    });
    
    io.on('connection', function(socket){
        socket.emit('notification', {message:"hi"});
    });
    
    http.listen(port, function(){
        console.log('listening on :' + port);
    });
    

提交回复
热议问题