首页 > 其他 > 详细

A secure socket.io connection

时间:2020-03-20 21:32:45      阅读:55      评论:0      收藏:0      [点我收藏+]

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 express@4.15.2
  • 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){ ...

技术分享图片
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

A secure socket.io connection

原文:https://www.cnblogs.com/sonictl/p/12533172.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!