簡體   English   中英

節點HTTP和WS服務器未按預期升級連接

[英]Node HTTP and WS server doesn't upgrade connection as expected

編輯:徹底檢查問題和示例,以嘗試縮小此范圍。 在本地開發中,我通過將wshttp分別由自己的服務器在自己的端口上進行處理來避免了這個問題,並且我遇到了一些嘗試在OpenShift上進行設置的問題,而我只能在單個端口上進行設置。

我正在嘗試使用OpenShift上的Node建立一個基於Websocket的簡單聊天系統,顯然我做錯了-我已經建立了一個簡單的服務器來按預期提供索引頁面,但是當該頁面請求Websocket時連接時,服務器嘗試將其作為常規HTTP請求進行應答,而不是按預期升級連接。

現場版

服務器:

 var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'; var port = process.env.OPENSHIFT_NODEJS_PORT || 8080; var ws = require( 'ws' ).Server; var http = require( 'http' ); var path = require( 'path' ); var fs = require( 'fs' ); // Create main HTTP server for the application var httpServer = http.createServer( function( request, response ){ console.log( 'REQUEST: ' + request.url ); var filename = path.join( process.cwd(), request.url ); fs.exists( filename, function( exists ){ fs.readFile( filename, 'binary', function( err, file ){ if ( err ){ console.log( 'FILE ERROR: ' + request.url ); response.writeHead( 500, {"Content-Type": "text/plain"} ); response.write( "500 - File Error\\n" + err + "\\n" ); response.end(); return; } response.writeHead( 200 ); response.write( file, 'binary' ); response.end(); } ); } ); } ).listen( port, ip, function(){ console.log( 'Server is listening at http://' + ip + ':' + port ); } ); // Create WebSocket server and begin listening wsServer = new ws( { server: httpServer } ); wsServer.on( 'connection', function( connection ){ console.log( 'New websocket connection...' ); connection.on( 'message', function( message ){ connection.set( 'Received: ' + message ); } ); connection.send( 'welcome!' ); }); 

客戶/索引頁:

 <!DOCTYPE html> <html> <head> <script> window.addEventListener( "load", function( event ){ var socket = new WebSocket( 'ws://exalted-instanttabletop.rhcloud.com'); } ); </script> </head> <body> <div id="log">Connecting...</div> </body> </html> 

這里是一個非常簡單的示例應用程序的鏈接,該應用程序使用相同的Node.js服務器來接受和處理WS和HTTP連接。 這應該可以幫助您入門: https : //github.com/openshift-quickstart/openshift-nodejs-http-and-websocket-example

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var WebSocketServer = require('ws').Server
var http = require('http');

var server = http.createServer(function (request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write("Welcome to Node.js on OpenShift!\n\n");
    response.end("Thanks for visiting us! \n");
});

server.listen(port, ipaddress, function () {
    console.log((new Date()) + ' Server is listening on port 8080');
});

wss = new WebSocketServer({
    server: server,
    autoAcceptConnections: false
});

wss.on('connection', function (ws) {
    console.log("New connection");
    ws.on('message', function (message) {
        ws.send("Received: " + message);
    });
    ws.send('Welcome!');
});

console.log("Listening to " + ipaddress + ":" + port + "...");

事實證明,我在這里的問題是,Openshift預定義了websocket連接必須進來端口8000 ,但隨后轉發到端口8080上我的裝備,所以我的服務器需要進行不同的端口上偵聽我客戶端已顯式連接,這是違反直覺的。

我在這里發現的一個幾乎不相關的問題最終使我引向Openshift一篇文章,其中詳細介紹了這些問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM