socket.io 대신 웹 소켓 용으로 자체 구현을 작성하고 있습니다.기본 Websocket 프레임을 어떻게 처리합니까?
나는 좋은 핸드 셰이크를 가지고 있지만 클라이언트가 서버로 보낼 때 유용한 데이터로 변환하는 방법을 알 수 없다. 그것은 대상입니까? 그것은 문자열인가? 워드 프로세서는 V8 힙 외부의 원시 메모리 위치 배열이라고합니다. ...?
(클라이언트 하드 문자열 코딩) 일례를 작동
var http = require("http");
var crypto = require("crypto");
var server = http.createServer();
server.on("upgrade", function (req, socket, upgradeHead) {
var crypto = require("crypto");
var shasum = crypto.createHash("sha1");
shasum.update(req.headers["sec-websocket-key"]);
shasum.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
var hash = shasum.digest("hex");
var myVal = new Buffer(hash, "hex").toString('base64');
var head = "";
head += "HTTP/1.1 101 Switching Protocols\r\n";
head += "Connection: Upgrade\r\n";
head += "Upgrade: WebSocket\r\n";
head += "Sec-WebSocket-Accept: " + myVal + "\r\n\r\n";
socket.setEncoding("utf8");
socket.write(head);
socket.ondata = function (data, start, end) {
var data = data.toString("utf8", start, end);
console.log(" start: " + start + "\n end: " + end + "\n data: " + data);
};
});
server.on("request", function (req, res) {
if (req.url === "/e")
process.exit();
if (req.url.indexOf("favicon") !== -1)
return;
var html = "\
<!DOCTYPE html>\r\n\
<html>\r\n\
<head>\r\n\
<script>\r\n\
var connection = new WebSocket('ws:localhost:80');\r\n\
connection.onopen = function() {\r\n\
console.log('OPEN SUCCESS');\r\n\
connection.send('I am a message from the client.');\r\n\
};\
connection.onmessage = function(msg) {\r\n\
console.log(msg);\r\n\
}\r\n\
connection.onerror = function (e) { console.log('ERROR'); console.log(e); };\r\n\
connection.onclose = function (e) { console.log('CLOSE'); console.log(e);};\r\n\
</script>\r\n\
</head>\r\n\
</html>";
res.writeHead(200, { "Content-Type": "text/html" });
res.write(html);
res.end();
});
server.listen(80);
node docs - socket.on(data, myFunc);
Detailed WebSocket Documentation
eazy-peezy wikipedia handshake explanation
남자, 당신은 나를 가장 어려운 부분으로 끌어 당기고 올바른 길로 가야합니다. 정말 고마워. 이것으로 많은 것을 배웠습니다. – user1873073
@ user1873073 문제가 없습니다! 나머지는 행운을 빌어 요. – loganfsmyth
나는 socket.write (payload.toString())로 보낸 사람에게 메시지를 다시 보내려고했다. 그리고 나는 (! Websocket 프로토콜 오류)있어. – ket