gpt4 book ai didi

javascript - Node HTTP 和 WS 服务器未按预期升级连接

转载 作者:行者123 更新时间:2023-11-28 15:28:09 25 4
gpt4 key购买 nike

编辑:彻底检查问题和示例,尝试缩小范围。在本地开发中,我通过让 wshttp 分别由自己的服务器在自己的端口上处理来避免这个问题,并且我在尝试设置时遇到了一些问题在 OpenShift 上,我仅限于单个端口。

我正在尝试使用 OpenShift 上的 Node 设置一个简单的基于 Websocket 的聊天系统,但我显然做错了一些事情 - 我设置了一个简单的服务器,它按预期提供我的索引页面,但是当该页面请求 Websocket 连接,服务器尝试将其作为常规 HTTP 请求进行应答,而不是按预期升级连接。

Live Version

服务器:

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 + "...");

关于javascript - Node HTTP 和 WS 服务器未按预期升级连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com