节点jsnetworking套接字+ websocket没有socket.io

我想用node.js创build类似char的东西我是nodejs的新手,我想创build它没有socket.io(我想了解它是如何工作的)。 这是我使用的代码。

var http = require('http'); var net = require('net'); var server = http.createServer(function(req,res){ res.writeHead(200,{'content-type' : 'text/html'}); res.write('<a href="./lol/">lol</a><br>'); res.end('hello world: '+req.url); var client = new net.Socket(); client.connect('7001', '127.0.0.1', function() { console.log('CONNECTED TO: '); // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client client.write('I am Chuck Norris!'); }); // Add a 'data' event handler for the client socket // data is what the server sent to this socket client.on('data', function(data) { console.log('DATA: ' + data); // Close the client socket completely client.destroy(); }); // Add a 'close' event handler for the client socket client.on('close', function() { console.log('Connection closed'); }); //req. }); server.listen(7000); require('net').createServer(function (socket) { console.log("connected"); socket.on('data', function (data) { console.log(data.toString()); }); }).listen(7001); 

所有的工作都很好,(我认为)。 当我打开本地主机:7000我收到节点CMD消息有关“连接到:”和“连接”和“我是查克诺里斯”。 之后,我试图在浏览器控制台中写入:

 var conn = new WebSocket('ws://localhost:7001/'); 

也没有错误,但是当我试着这一行:

 conn.send('lol'); 

我得到错误:“未被捕获的DOMException:未能执行”发送“'WebSocket':仍处于连接状态。(…)”

并在一段时间后,我又得到一个错误:“WebSocket连接到'ws:// localhost:7001 /'失败:WebSocket打开握手超时”

也许这个代码是错误的,但我已经尝试了一切,我发现扔谷歌。 有人可以帮我弄这个吗?

如果你想创build自己的webSocket服务器,可以从浏览器接收webSocket连接,你必须在你的服务器上实现webSocket协议。 这不仅仅是一个简单的套接字连接。 它有一个启动顺序,首先作为HTTP连接,然后“升级”到webSocket协议,包括交换安全信息,然后通过webSocket发送所有数据的webSocket成帧格式。 你不只是通过webSocket发送纯文本。

您可以在这里看到webSocket协议的样子: 编写Websocket服务器 。 除非你真的想把自己的webSocket服务器仅仅用于学习目的,否则我真的build议你得到一个已经完成了所有基本协议的模块。

然后,socket.io库build立在webSocket协议的基础之上,在其上添加额外的特性和消息格式。

为了让你知道一个webSocket如何连接,下面是一个典型的连接顺序:

浏览器发送连接请求:

 GET /chat HTTP/1.1 Host: example.com:8000 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Version: 13 

服务器响应:

 HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

然后,双方切换到这样的数据成帧格式的webSocket协议:

 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ 

此外,还有用于保持活动testing的ping和pong数据包,并且存在用于大数据包和分段的scheme,并且客户端/服务器可以协商子协议。