A secure socket.io connection

node-js-socket-io-with-ssl:
https://stackoverflow.com/questions/6599470/node-js-socket-io-with-ssl?noredirect=1&lq=1

1. make a simplest socket.io app

ref: https://socket.io/get-started/chat/
#### basic nodejs app:

  • use the Node.JS web framework express to html as client end.
  • create a package.json manifest file that describes our project
  • npm install [email protected]
  • create an index.js file that will setup our application
    The index.js file supply the html content of "Hello world".
  • create a index.html file and modify the index.js to serve the index.html.

#### Insert socket.io:
The socket.io has a service end (socket.io) and client end (socket.io-client).

  • install: npm install socket.io
  • in index.js, initialize a new instance of socket.io by passing the http (the HTTP server) object.
  • listen on the connection event for incoming sockets, and I log it to the console.
  • in index.html, add the snippet about var socket = io(); for loading the socket.io-client.

#### Emitting events:
Send and receive any events we want, with any data. Any objects that can be encoded as JSON will do, and binary data is supported too.

  • make the server get user message as a chat message event, add the emitting code in index.html.
  • in index.js we print out the chat message event

#### Broadcasting:

  • send the message to everyone, including the sender: io.emit('chat message', msg);
  • in client, make it capture a chat message event: socket.on('chat message', function(msg){ ...

chat pages
For more details: https://socket.io/get-started/chat/

2. Take nginx as a web server, make the nodejs app accessible

Even though the Caddy is the convenient server for https and the certificate application. The nginx is still the wider used solution for web service.

3. Secure the nodejs app by https

4. Secure the socket.io

猜你喜欢

转载自www.cnblogs.com/sonictl/p/12533172.html