在nodejs socket.io中显示连续连接消息 [英] Show continuous connection message in nodejs socket.io

查看:77
本文介绍了在nodejs socket.io中显示连续连接消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel开发实时聊天应用程序.我遇到了问题.当我运行"node index.js"时,在命令提示符下连续显示已建立连接"消息.

I am trying to develop a live chat application using laravel. I face a problem. When I run "node index.js" , 'A connection has made' message has shown continuously in command prompt.

我的index.js文件是:

my index.js file is:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);
app.get('/', function(request, response){
    response.sendFile(__dirname+ '/index.html');        
});

io.on('connection', function(socket){
    console.log('A connection has made');
    // socket.on('chat.message', function(message){
    //  io.emit('chat.message', message);
    // });
});

我的index.html页面是:

My index.html page is:

<!DOCTYPE html>
<html>
    <head>
        <title>Live Chat</title>
    </head>
    <body>
        <div class="container" id="chat">
            <h1> Chat System </h1>
        </div>
    <script type="text/javascript">
    var socket = io();
    </script>
    </body>
</html>

我该如何解决?

推荐答案

您的客户端不断尝试一遍又一遍地进行连接的通常原因是,因为您使用的socket.io客户端和服务器版本不匹配,导致它们不兼容.您没有显示如何在网页中加载socket.io Javascript,但是如果您这样做的话:

The usual reason that your client is continually trying to connect over and over again is because you have a mismatched client and server version of socket.io making them incompatible. You don't show how you load the socket.io Javascript in your web page, but if you do it like this:

<script src="/socket.io/socket.io.js"></script>

然后,您将始终从服务器自动获得与服务器完全匹配的版本(这是socket.io服务器自动添加到Express服务器的路由).

Then, you will always get the version that exactly matches your server from your server automatically (this is a route that the socket.io server automatically adds to your Express server).

如果要从CDN加载socket.io,则必须切换到上述内容以从您自己的服务器加载它,或者从CDN手动指定与服务器上运行的版本完全相同的版本.

If you are loading socket.io from a CDN, then you have to either switch to the above to load it from your own server or manually specify the exact same version from the CDN as you are running on the server.

这篇关于在nodejs socket.io中显示连续连接消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆