pomelo icon indicating copy to clipboard operation
pomelo copied to clipboard

Websocket with ssl

Open nhatminh12369 opened this issue 12 years ago • 9 comments

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.

nhatminh12369 avatar Jun 05 '14 09:06 nhatminh12369

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:// .

py8765 avatar Jun 06 '14 01:06 py8765

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.

nhatminh12369 avatar Jun 07 '14 02:06 nhatminh12369

If you use the js client, and you cannot use tls. You can try the command pomelo init first, and choose the wss type.

py8765 avatar Jun 09 '14 01:06 py8765

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. (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\lib\components\connector.js:223:29) at EventEmitter.emit (events.js:95:17) at handleData (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\lib\connectors\common\handler.js:38:9) at handle (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\lib\connectors\common\handler.js:49:5) at WebSocket. (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\lib\connectors\hybridsocket.js:41:7) at WebSocket.EventEmitter.emit (events.js:98:17) at Receiver.self._receiver.onbinary (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\node_modules\ws\lib\WebSocket.js:549:10) at Receiver.opcodes.2.finish (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\node_modules\ws\lib\Receiver.js:448:14) at Receiver.expectHandler (D:\TwinsBlitz\win\tbserver\game-server\node_modules\pomelo\node_modules\ws\lib\Receiver.js:433:33)

Do you have any idea about this error? Thanks

nhatminh12369 avatar Jun 09 '14 15:06 nhatminh12369

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.

py8765 avatar Jun 10 '14 03:06 py8765

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

py8765 avatar Jun 10 '14 03:06 py8765

Yes, I have tried that and it works! Thanks for your help.

nhatminh12369 avatar Jun 10 '14 05:06 nhatminh12369

由于自己的服务器有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

NextZeus avatar Dec 14 '17 08:12 NextZeus

现在用了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要处理些什么吗

innocentjulie avatar Jul 30 '18 09:07 innocentjulie