0
我摔倒了websocket和http。爲什麼websocket連接創建不通過http?
我已經寫了下面的例子:
var fs = require('fs');
var server = http.createServer(function(request, response) {
console.log ("HTTP Request created...");
// I am responding something here..
});
server.listen(1234, function() {
console.log((new Date()) + ' Server is listening on port 1234');
});
var WebSocketServer = require('websocket').server;
wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on('request', function(re){
var conn = re.accept(null, re.origin);
console.log ("Accepted New Connection..");
conn.on('message', function(message) {
console.log ('message received' + message.utf8Data);
});
});
我在連接到該服務器的兩種方式嘗試。
1) Through Browser.
2) through node.js application
當我試圖達到通過瀏覽器前該服務器:http:IP:1234, 我得到「HTTP請求接受。」被打印出來,在那裏,當我與 嘗試下面的代碼中的Node.js,我沒有看到打印這條消息。
var WebSocket = require('ws')
ws = new WebSocket('ws://IP:1234');
ws.on('open', function() {
console.log ("WebSocket opened..");
ws.send('something');
});
ws.on('message', function(message) {
console.log('received: %s', message.data);
});
當我試圖連接通過
WS到網絡服務器=新的WebSocket( 'WS:// IP:1234');
爲什麼它沒有通過HTTP?。我的基本理解是Websocket只是HTTP之上的升級,在這種情況下,我會假設WebSocket()依次通過HTTP連接到服務器。還是我困惑?