Websocket with ssl
I saw that Pomelo has add this feature into version 1.0.0 pre, right? Based on a note in History.md: connector wss & tls support
How can I enable websocket secure (wss)? Thanks.
yes, we support wss in 1.0.0 pre. In server side, you should configure app.js
app.configure('production|development', 'connector', function() {
app.set('connectorConfig',
{
connector : pomelo.connectors.hybridconnector,
heartbeat : 3,
useDict : true,
useProtobuf : true,
ssl: {
type: 'wss',
key: fs.readFileSync('./keys/server.key'),
cert: fs.readFileSync('./keys/server.crt'),
}
});
});
In client side you just change the url ws:// to wss:// .
For the client side, I'm using the build.js (chatofpomelo-websocket\web-server\public\js\lib\build\build.js). It just allows me to specify host and port. It means to change the url from ws:// to wss://, I have to do it inside the build.js file. I have tried it but the server seems to receive nothing.
If I change the ssl type to 'tls', the server will receive a connection and immediately close it. [2014-06-07 09:56:29.557] [connector-server-1] getSession session is created with session id: 1 [2014-06-07 09:56:29.560] session on [connector-server-1] is closed with session id: 1
Please help me to use the client library, thanks.
If you use the js client, and you cannot use tls. You can try the command pomelo init first, and choose the wss type.
At first, I tried pomelo init using js client and didn't received anything. After that, I tried to add a ca-bundle into the ssl config (I have tried the ssl config with https in nodejs and it works fine) :
ssl: {
type: 'wss',
ca: caFiles,
key: fs.readFileSync('./keys/server.key'),
cert: fs.readFileSync('./keys/server.crt')
}
Now, the pomelo client can Init the connection but then, the server crashed:
Caught exception : TypeError: Cannot read property 'useDict' of undefined
at decode (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\lib\connectors\common\coder.js:19:24)
at null.
Do you have any idea about this error? Thanks
Do you use the server and client that we provide in pomelo init? And if you use wss in demo, you do not need ca-bundle.
You can install the latest pomelo, and use the command pomelo init yourprojectname, choose the third option. After starting the game-server and the web-server, you can login https://127.0.0.1:3001
Yes, I have tried that and it works! Thanks for your help.
由于自己的服务器有gate , connector 两种front服务器,所有就选择这样配置。
app.configure('production|development', 'connector|gate', function() {
app.set('connectorConfig',
{
connector : pomelo.connectors.hybridconnector,
heartbeat : 3,
useDict : true,
useProtobuf : true,
ssl: {
type: 'wss',
key: fs.readFileSync('./keys/server.key'),
cert: fs.readFileSync('./keys/server.crt'),
}
});
});
这种方式如果没有配合nginx 的话, 只能通过ip+port直连。
nginx 配置wss
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream gate {
server 127.0.0.1:3014;
}
server {
listen 443;
server_name pomelo.game.com;
ssl on;
ssl_certificate cert.crt;
ssl_certificate_key cert.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols TLSV1.1 TLSV1.2 SSLv2 SSLv3;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location /gate {
proxy_pass http://gate;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#charset koi8-r;
access_log /var/log/nginx/log/xxx.access.log main;
location / {
root /data/www/xxxx;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
}
# 客户端访问 wss//pomelo.game.com/gate
现在用了ip+端口直连,还是连不上,是不是我 key: fs.readFileSync('./keys/server.key'), cert: fs.readFileSync('./keys/server.crt'), 这个证书有问题?我网站上用的是另外一个证书,pomelo用的是share自定义的一个证书, 连接的时候一直报错:WebSocket connection to 'ws://127.0.0.1:3015/' failed: Connection closed before receiving a handshake response 或者 Error in connection establishment: net::ERR_CONNECTION_CLOSED,服务端收不到任何消息,是https要处理些什么吗