本文介绍了Node js - Socket.io-client 未连接到 socket.io 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码连接到 socket.io 客户端:

服务器:

//加载需求var http = require('http'),io = require('socket.io');//创建服务器 &插座var server = http.createServer(function(req, res){//发送 HTML 标题和消息res.writeHead(404, {'Content-Type': 'text/html'});res.end('<h1>啊,啪!404</h1>');});服务器.听(8080);io = io.listen(服务器);//添加一个连接监听器io.sockets.on('connection', function(socket) {console.log('客户端已连接.');//断开监听器socket.on('断开', function() {console.log('客户端断开连接.');});});

客户:

console.log('1');//连接到服务器var io = require('socket.io-client')var socket = io.connect('localhost:8080', {reconnect: true});console.log('2');//添加一个连接监听器socket.on('connect', function(socket) {console.log('已连接!');});console.log('3');

我没有得到连接控制台日志或客户端连接控制台日志,我不知道为什么!代码示例取自发布的另一个问题:链接 我没有看到任何解决问题的方法...

解决方案

假设您使用的是大于 1.0 的 socket.io 版本,在服务器上,更改此:

//添加连接监听器io.sockets.on('connection', function(socket) {console.log('客户端已连接.');//断开监听器socket.on('断开', function() {console.log('客户端断开连接.');});});

为此:

//添加连接监听器io.on('connection', function(socket) {console.log('客户端已连接.');//断开监听器socket.on('断开', function() {console.log('客户端断开连接.');});});

请参阅 socket.io 文档参考此处.>

您不想仅在已连接的套接字上监听此事件.您想在任何套接字上侦听此事件,甚至是新创建的套接字.

另外,在互联网上随机位置阅读 socket.io 代码时要非常小心.有些东西从 v0.9 到 v1.0 发生了显着变化(我不知道这是否是其中之一).您通常应该首先从 socket.io 文档站点开始,因为它始终代表最新版本.然后,如果查看其他互联网参考资料,请确保您只使用 2014 年年中之后的文章.如果你不知道一篇文章的年代,最好不要在没有最近文章证实的情况下依赖它.

I am trying to connect to a socket.io-client using the following code:

Server:

// Load requirements
var http = require('http'),
    io = require('socket.io');

// Create server & socket
var server = http.createServer(function(req, res){

    // Send HTML headers and message
    res.writeHead(404, {'Content-Type': 'text/html'});
    res.end('<h1>Aw, snap! 404</h1>');
});
server.listen(8080);
io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

Client:

console.log('1');

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});

console.log('2');

// Add a connect listener
socket.on('connect', function(socket) {
    console.log('Connected!');
});

console.log('3');

I don't get the Connected console log or Client Connected console log and I don't know why! The code sample is taken from another question posted: Link and I don't see any solution to the problem...

解决方案

Assuming you are using a socket.io version greater than 1.0, on the server, change this:

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

to this:

// Add a connect listener
io.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

See the socket.io documentation reference here.


You don't want to be listening for this event only on already connected sockets. You want to listen for this event on any socket, even a newly created one.


Also, be very careful when reading socket.io code in random places on the internet. Some things changed significantly from v0.9 to v1.0 (I don't know if this was one of those things or not). You should generally always start with the socket.io documentation site first since that will always represent the latest version. Then, if looking at other internet references, make sure you only use articles that are later than mid-2014. If you don't know the vintage of an article, it's best not to rely on it without corroboration from a more recent article.

这篇关于Node js - Socket.io-client 未连接到 socket.io 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:55