Socket.io 404 error

前端 未结 4 1652
心在旅途
心在旅途 2020-12-22 04:12

I have followed Jeffrey way\'s tutorial.
Tutorial:
https://laracasts.com/series/real-time-laravel-with-socket-io/episodes/1

this is my index.js



        
4条回答
  •  抹茶落季
    2020-12-22 05:04

    Try doing this code.

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    
    app.get('/', function(req, res) {
        res.sendfile('index.html');
    });
    
    users = [];
    io.on('connection', function(socket) {
        console.log('A user connected');
        socket.on('setUsername', function(data) {
            console.log(data);
    
            if(users.indexOf(data) > -1) {
                socket.emit('userExists', data + ' username is taken! Try some other username.');
            } else {
                users.push(data);
                socket.emit('userSet', {username: data});
            }
        });
    
        socket.on('msg', function(data) {
            //Send message to everyone
            io.sockets.emit('newmsg', data);
        })
    });
    
    http.listen(3000, function() {
       console.log('listening on localhost:3000');
    });
    

    Let me know if it works.

提交回复
热议问题