如何通过socket.io?

时间:2016-05-09 14:59:51

标签: node.js ssl websocket socket.io

在没有ssl连接的情况下,一切正常。

我在设置之后得到的回复:
socket.io-1.4.5.js:1
GET https://domain.com:3000/socket.io/?EIO=3&transport=polling&t=LILwU3l net::ERR_INSECURE_RESPONSE

服务器

node.js v.6.1

的package.json

{
  //...,
  "dependencies":
  {
    "socket.io": "1.4.5"
  }
}

server.js

查看如果仅声明端口,socket.io如何解决创建服务器的问题:https://github.com/socketio/socket.io/blob/master/lib/index.js#L212

const
  fs      = require('fs'),
  https   = require('https').createServer(
    {
      // certificate by godaddy (G2)
      key : fs.readFileSync('/etc/ssl/private/domain.key'),
      cert: fs.readFileSync('/etc/ssl/certs/domain.crt')
    },
    (req, res) =>
    {
      res.writeHead(404);
      res.end();
    }).listen(3000),
  io      = require('socket.io')(https);

io.on('connection', (socket) => {console.log('CONNECTED');});

客户端

require.config(
{
  shim:
  {
    'socketio':
    {
      exports: 'io'
    }
  },
  paths:
  {
    socketio: 'https://cdn.socket.io/socket.io-1.4.5'
  }
});

define(['socketio'], function(io)
{
  var socket = io.connect('https://domain.com:3000');
});

SSL验证

$ openssl s_client -CApath /etc/ssl/certs/ -connect domain.com:3000 -showcerts
...
Verify return code: 0 (ok)

0 个答案:

没有答案