我已经创建了一个NodeJS websocket服务器和客户端
服务器:
//handler server
app.get('/server', function (req, res) {
//test websocket
console.log("server: starting websocket server...");
var port = (process.env.PORT || 8888);
var WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: port});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('server: received: %s', message);
ws.send('echo: ' + message);
});
ws.send('connected');
});
console.log("server: listening websocket on " + appEnv.url + " - port " + port );
res.send('SERVER - listening websocket on: ' + appEnv.url + " port " + port);
});
客户端
//handler client
app.get('/client', function (req, res) {
//var url = 'ws://localhost:8888';//local
var url = 'ws://<appname>.mybluemix.net';//BlueMix
var WebSocket = require('ws')
, ws = new WebSocket(url);
console.log('client: calling url: %s', url);
ws.on('open', function() {
ws.send('hello');
});
ws.on('message', function(message) {
console.log('client: received: %s', message);
});
res.send('CLIENT - WebSocket call done - check the log !!!');
});
我的期望是使用BlueMix上的env VCAP_APP_port/port提供的端口。但我得到了服务器错误:侦听EADDRINUSE。这是从NodeJS运行时服务器使用的端口的合理原因。
本地所有其他端口都可以工作(80/443/8888/5555/XXXX等(,但客户端调用必须在该端口上完成:
本地测试:
服务器localhost:607-端口80>OK--客户端调用ws://localhost>OK
服务器localhost:607-端口433>OK--客户端调用ws://localhost:443>OK
服务器localhost:607-端口8888>确定--客户端调用ws://localhost:8888>确定
SERVER localhost:607-端口5555>OK--CLIENT调用ws://localhost:55555>确定
在BlueMix上,使用VCAP_APP_PORT是不可能的,因为该端口是从NodeJS服务器使用的(错误EADDRINUSE(。似乎不允许使用端口80/443(错误EACCES权限被拒绝(。使用所有其他端口在服务器端工作。但是BlueMix防火墙在传入呼叫时阻塞了除80/443之外的所有端口(这是使用Websocket而不是Socket的原因(。
关于BLUEMIX:的测试
SERVER VCAP_APP_PORT(62577-62045(>服务器错误:侦听EADDRINUSE
SERVER端口80/443>服务器错误:侦听EACCES(权限被拒绝(
服务器端口8888>确定--客户端调用ws://myapp.mybluemix.net:888>错误:连接ECONNREFUSED
SERVER端口8888>OK--CLIENT调用ws://myapp.mybluemix.net>错误:意外的服务器响应(500(
SERVER端口5555>OK--CLIENT调用ws://myapp.mybluemix.net>错误:意外的服务器响应(500(
服务器端口5555>正常--客户端调用ws://myapp.mybluemix.net:5555>错误:连接ECONNREFUSED
1.Wich端口必须在BlueMix上使用
2.客户端调用必须在80/443上工作,Websocket是为在80(http(和443(https(端口上处理传入调用而创建的
感谢
使用现有Express应用程序启动WebSocket服务器需要共享连接实例,而不是直接打开。
从他们的文档中,使用服务器参数而不是端口创建WebSocketServer对象。请参阅下面的示例。
平台路由器接收到的HTTP和WS流量都将使用port变量转发到应用程序绑定到的内部端口。
var server=require('http'(.createServer((,url=require('url'(,WebSocketServer=require('ws'(。服务器,wss=新WebSocketServer({server:server}(,express=require('express'(,app=express((,端口=4080;app.use(函数(req,res({res.send({msg:"你好"}(;});wss.on("连接",函数连接(ws({var location=url.parse(ws.upgradeReq.url,true(;//您可以使用location.query.access_token进行身份验证或共享会话//或ws.upgradeReq.headers.cookie(请参阅http://stackoverflow.com/a/16395220/151312)ws.on('message',函数传入(message({console.log("已接收:%s",消息(;});ws.send("something"(;});server.on("请求",应用程序(;server.listen(port,function(({console.log('登录'+server.address((.port(}(;
非常感谢@James Thomas按照您在BlueMix上的建议尝试了乒乓球WS。它有效!!
var server = require('http').createServer()
, url = require('url')
, WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ server: server })
, express = require('express')
, app = express()
, port = (process.env.PORT || 4080);
//---------------------------------------------------------------------------
wss.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
ws.on('message', function incoming(message) {
console.log('server: on message - received: %s', message);
});
console.log('server: waiting connection');
ws.send('msg from server');
});
//---------------------------------------------------------------------------
app.use(function (req, res) {
//var url = 'ws://localhost:4080';
var url = 'ws://<app>.mybluemix.net';
var WebSocket = require('ws')
, ws = new WebSocket(url);
console.log('client: calling url: %s', url);
ws.on('open', function() {
ws.send('msg from client');
console.log('client: on open - sent msg from client');
});
ws.on('message', function(message) {
console.log('client: on messagge - received: %s', message);
});
res.send({ msg: "client call done" });
});
//---------------------------------------------------------------------------
server.on('request', app);
server.listen(port, function () { console.log('Listening on ' + server.address().port) })