Ілл*_*роз 13 javascript sockets websocket node.js
我尝试使用node.js创建类似char的东西我是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);
Run Code Online (Sandbox Code Playgroud)
一切正常,(我认为).当我打开localhost:7000时,我收到节点CMD消息"CONNECTED TO:"和"connected"以及"我是Chack Norris".之后,我正在尝试在浏览器控制台中写入:
var conn = new WebSocket('ws://localhost:7001/');
Run Code Online (Sandbox Code Playgroud)
也没有错误,但是当我尝试这一行时:
conn.send('lol');
Run Code Online (Sandbox Code Playgroud)
我收到错误:"未捕获DOMException:无法在'WebSocket'上执行'发送':仍然处于CONNECTING状态.(...)"
过了一段时间我又得到了一个错误:"WebSocket连接到'ws:// localhost:7001 /'失败:WebSocket打开握手超时"
也许这个代码是错误的,但我已经尝试了我发现扔掉谷歌的一切.有人可以帮我弄这个吗?
jfr*_*d00 20
如果要创建自己的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
Run Code Online (Sandbox Code Playgroud)
服务器响应:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Run Code Online (Sandbox Code Playgroud)
然后,双方切换到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 ... |
+---------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
然后,此外,有用于保持活动测试的ping和pong分组,并且存在用于大分组和分段的方案,并且客户端/服务器可以协商子协议.
| 归档时间: |
|
| 查看次数: |
4687 次 |
| 最近记录: |