繁体   English   中英

Node.js Socket.io 尝试将客户端连接到服务器时出错

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

我正在尝试创建一个 Web 聊天,首先我们需要通过 Node 和 Socket.io 设置客户端到服务器的连接 - 它不起作用。

服务器代码在这里: https://collectiveboost.com/cb_node/chat_app/server.js

它通过以下方式启动时运行良好:node server.js 将结果打印到 putty 命令行:“Socket io Server Chat Started...”

但是在客户端,socket.io 连接没有发生:(我们已经尝试过两种方式,即通过全局 socket.io,正如您在此处看到的: https/challective.boostindex.

这导致错误:加载资源失败:服务器响应状态为 404(未找到)未捕获参考错误:io 未在(索引)定义:14

并通过客户端 JS 文件的本地版本,可以通过以下方式访问:node_modules/socket.io/client-dist/socket.io.js,如下所述: https://socket.io/get-started/chat

你可以在这里看到: https://collectiveboost.com/cb_node/chat_app/index_2.htm

这会导致错误:polling-xhr.js:157 GET https://collectiveboost.com/socket.io/?EIO=4&transport=polling&t=Nt9UN_R 404(未找到)

那么我们做错了什么? 我们如何将客户端 Web 页面 socket.io 连接到服务器?

谢谢


请尝试此代码,它对您非常有用。 它是一个基本的简单聊天系统。

服务器代码

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');
    });

客户代码

 <.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>

暂无
暂无

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

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