简体   繁体   English

NodeJS和SocketIO入门困难

[英]Trouble getting started with NodeJS and SocketIO

I'm trying to get a basic structure going using socket.io and node.js while implementing the tcp functionality shown on the home-page of node.js using the ('net') to create a server. 我正在尝试使用socket.ionode.js获得一种基本结构,同时使用('net')创建node.js主页上显示的tcp功能,以创建服务器。

As there's not much code, here's what I have so far 由于没有太多代码,这是我到目前为止的内容

Server: 服务器:

// Setup the server
var tcpProtocol = require('net');
var serverListener = tcpProtocol.createServer(tcpHandler);
var socketIO = require('socket.io').listen(serverListener);

function tcpHandler(socket) {
    console.log("TCP Handler received a call.");
    socket.pipe(socket);
}

socketIO.sockets.on('connection', function(socket) {
    console.log("Connection maid.");
});

socketIO.sockets.on('login', function(username, password) {
    console.log("login called.");
    console.log('username: ' + username);
    console.log('password: ' + password);
});


serverListener.listen(5055, '127.0.0.1');

console.log("Server started on port 5055");

Then ofcourse, the client: 然后,客户当然会:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Test Client</title>
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>
</head>
<body>
    <script>
        var socket = io.connect('http://localhost:5055');
        $(function() {
          socket.emit('login', 'Chris', 'PassBob');
            console.log("sent");
        });
    </script>
</body>
</html>

The server is printing out the call for the tcpHandler, however the socketIO calls aren't getting any recognition. 服务器正在打印对tcpHandler的调用,但是socketIO调用未得到任何识别。 The sent from the client is printed, however I notice that the tcpHandler call gets called repetitively a few times, like it's still attempting to send the data after it failed or something? 从客户端sent信息已打印出来,但是我注意到tcpHandler调用被重复调用了几次,就像它仍然在尝试发送数据失败后还是尝试发送数据? Just an assumption. 只是一个假设。

You should handle your sockets logic inside the connection event. 您应该在connection事件内部处理套接字逻辑。

Your code should look like this: 您的代码应如下所示:

Server 服务器

socketIO.sockets.on('connection', function(socket) {
    console.log("Connection maid.");

    socket.on('login', function(username, password) {
        console.log("login called.");
        console.log('username: ' + username);
        console.log('password: ' + password);
    });
});

You can refer to this open-source project - it has lot of sockets logic inside (highlighted) 您可以参考这个开源项目-内部有很多套接字逻辑(突出显示)

https://github.com/thecatontheflat/agile-estimation/blob/0.0.3/server.js#L94-L196 https://github.com/thecatontheflat/agile-estimation/blob/0.0.3/server.js#L94-L196

Even though the code is dirty, I believe you can get lot of useful examples from there 即使代码很脏,我相信您也可以从那里获得许多有用的示例

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

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