[英]nodejs net sockets + websocket without socket.io
我正在嘗試使用 nodejs 創建類似聊天的東西。 我是 nodejs 的新手,我想在沒有 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);
一切正常,(我認為)。 當我打開 localhost:7000 時,我會收到有關“CONNECTED TO:”和“已連接”以及“我是 Chack Norris”的節點 CMD 消息。 之后,我嘗試在瀏覽器控制台中編寫:
var conn = new WebSocket('ws://localhost:7001/');
也沒有錯誤,但是當我嘗試這一行時:
conn.send('lol');
我收到一個錯誤:“未捕獲的 DOMException:無法在‘WebSocket’上執行‘發送’:仍處於連接狀態。(...)”
一段時間后,我又收到一個錯誤:“與 'ws://localhost:7001/' 的 WebSocket 連接失敗:WebSocket 打開握手超時”
也許這段代碼是錯誤的,但我已經嘗試了通過谷歌找到的所有內容。 有人可以幫我弄這個嗎?
如果你想創建自己的 webSocket 服務器,可以從瀏覽器接收 webSocket 連接,你必須在你的服務器上實現 webSocket 協議。 它不僅僅是一個簡單的套接字連接。 它有一個啟動序列,從一個 HTTP 連接開始,然后“升級”到 webSocket 協議,包括安全信息的交換,然后有一個 webSocket 幀格式,用於通過 webSocket 發送的所有數據。 您不只是通過 webSocket 發送純文本。
您可以在此處查看 webSocket 協議的樣子: 編寫 Websocket 服務器。 除非你真的想制作你自己的 webSocket 服務器只是為了學習目的,否則我真的建議你獲得一個現有的模塊,它已經為你完成了所有的細節協議工作。
然后 socket.io 庫建立在 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 ... |
+---------------------------------------------------------------+
然后,此外,還有用於保活測試的 ping 和 pong 數據包,還有用於大數據包和分片的方案,客戶端/服務器可以協商子協議。
如上所述,除非您確實需要/想要,否則最好不要編寫自己的套接字服務器。
具有簡單界面的出色套接字服務器是ws
https://github.com/websockets/ws
只需幾行,您就可以開始在服務器端發送和接收消息。
同時,在客戶端,您不需要庫,只需:
new WebSocket('wss//echo.websocket.org')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.