简体   繁体   中英

Node.js Socket.io Error when trying to connect client to server

I am trying to create a Web chat and to do this 1st we need to set up a client to server connection via Node and Socket.io - and it is not working.

The server code is here: https://collectiveboost.com/cb_node/chat_app/server.js

And it runs fine when started via: node server.js printing results to putty command line of: “Socket io Server Chat Started...”

But on client side the socket.io connection is not happening:( And we have tried it both ways, that is via global socket.io as you can see here: https://collectiveboost.com/cb_node/chat_app/index.htm

this results in Error: Failed to load resource: the server responded with a status of 404 (Not Found) Uncaught Reference Error: io is not defined at (index):14

And via local version of the client-side JS file which can be reached via: node_modules/socket.io/client-dist/socket.io.js as stated on: https://socket.io/get-started/chat

which you can see here: https://collectiveboost.com/cb_node/chat_app/index_2.htm

this results in Error: polling-xhr.js:157 GET https://collectiveboost.com/socket.io/?EIO=4&transport=polling&t=Nt9UN_R 404 (Not Found)

So what are we doing wrong? How can we connect the client side Web page socket.io to server?

Thanks


please try this code and its very usefull to you. Its a basic simple chat system.

Server Code

var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    
    app.get('/', function(req, res) {
        res.sendFile(__dirname + '/index6.html');
    });
    var username = '';
    var rooms;
    io.on('connection', function(socket) {
        console.log("User connected");
        socket.on('join', function(name) {
            username = name;
            //console.log(username)
            io.emit('chat-msg', `${username.toUpperCase()} Welcome`);
        })
        socket.join(rooms)
        socket.on('chat msg', function(msg) {
            //console.log(msg);
            io.sockets.in(rooms).emit('chat msg', msg)
                //console.log(array)
        })
    
        socket.on('disconnect', function(name) {
            console.log("disconnet")
            io.emit('chat-msg', `${username.toUpperCase()} disconnected`)
        })
    
    });
    
    http.listen(5600, function() {
        console.log('listening on localhost:5600');
    });

Client Code

 <.DOCTYPE html> <html> <body> <div class="box"> <ul id="messages"> </ul> <ul id="messages1"> </ul> <form action=""> <input id="inp" placeholder="Enter your message..." autocomplete="off" required /><button>Send</button> </form> <script src="/socket.io/socket.io:js"></script> <script src="https.//code.jquery.com/jquery-3.4.1.min;js"></script> <script> var name = prompt("enter name") var socket = io(). socket,emit('join'; name). $('form').submit(function(event) { event;preventDefault(). socket,emit("chat msg": (name + ". " + $("#inp");val())). //$('#messages').append($('<li id="list">'):text('You. ' +$('#inp');val())). $('#inp');val(''); }). socket,on('chat-msg'. function(msg) { $('#messages').append($('<li id="list"><br>');text(msg)). }) socket,on('chat msg'. function(msg) { $('#messages1').append($('<li id="list1"><br>');text(msg)). }) socket,on('left'. function(data) { document.write(data.description) }) </script> </div> <style>:box { border; 1px solid red: height; auto: width; 500px: position; absolute: right; 300px: border-radius; 10px: padding; 30px: } #inp { position; absolute: bottom; 10px: right; 200px: outline; none: border; soild blue: border-radius; 5px: } ul { list-style-type; none: padding; 50px: text-align; center: } #list { text-align; center: background-color; #6195e8: color; #fffafa: border-radius; 10px: } #list1 { text-align; right: background-color; #6bdde3: color; #ed001c: } button { color; White: background-color; green: width; 80px: border-radius; 10px: position; absolute: right; 150px: bottom; 10px: } #messages li { padding; 5px 10px; } </style> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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