을 포함해야한다. 나는 방금 emarrassingly 긴 시간 동안 이걸로 고투하고 간단한 솔루션을 공유 줄 알았는데. 또한 @Dibu Raj가 회신 함과 동시에 handleUpgrades 옵션을 true로 설정하여 서버를 다시 만들어야합니다. ./static/test/index에서 파일에 아래이 HTML 쓰기 : 새 nodejs의 웹 소켓 서버를 테스트 할 수있는 HTML 페이지의 경우
'use strict';
var restify = require('restify');
var watershed = require('watershed');
var ws = new watershed.Watershed();
var server = restify.createServer({
handleUpgrades: true
});
server.get('/websocket/attach', function (req, res, next) {
if (!res.claimUpgrade) {
next(new Error('Connection Must Upgrade For WebSockets'));
return;
}
console.log("upgrade claimed");
var upgrade = res.claimUpgrade();
var shed = ws.accept(req, upgrade.socket, upgrade.head);
shed.on('text', function(msg) {
console.log('Received message from websocket client: ' + msg);
});
shed.send('hello there!');
next(false);
});
//For a complete sample, here is an ability to serve up a subfolder:
server.get(/\/test\/?.*/, restify.serveStatic({
directory: './static',
default: 'index.html'
}));
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
: 여기에 웹 소켓 업그레이드 유역에 restify 작품을 만들 수있는 완벽한 예입니다. html - 브라우저에서 http://localhost:8080/test/index.html을 가리키고 - 브라우저 디버그 콘솔을 열어 메시지 교환을 확인하십시오.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web Socket test area</title>
<meta name="description" content="Web Socket tester">
<meta name="author" content="Tim">
</head>
<body>
Test Text.
<script>
(function() {
console.log("Opening connection");
var exampleSocket = new WebSocket("ws:/localhost:8080/websocket/attach");
exampleSocket.onopen = function (event) {
console.log("Opened socket!");
exampleSocket.send("Here's some text that the server is urgently awaiting!");
};
exampleSocket.onmessage = function (event) {
console.log("return:", event.data);
exampleSocket.close();
}
})();
</script>
</body>
</html>
브라우저 로그는 다음과 같이 보일 것이다 :
07:05:05.357 index.html:18 Opening connection
07:05:05.480 index.html:22 Opened socket!
07:05:05.481 index.html:26 return: hello there!
을 그리고 노드 로그는 다음과 같다 : http://restify.com/#upgrade-requests
방법에서 찾을 이것에 대한
문서 너 알아? 그런 것들을 어디서 읽나요? –