Socket.io客户端:Websocket连接失败:连接在收到握手响应之前关闭

时间:2017-10-24 21:52:51

标签: node.js express socket.io

我正在构建一个Web应用程序(nodejs,port:xxx0),它与作为socket.io服务器的另一个应用程序(nodejs,port:xxx5)进行交互。我的socket.io客户端代码在我的网络应用程序中运行良好。一切都在我的本地很好用,因为连接将服务器称为io.connect('http://localhost:xxx5/')

但是,当我将其提升到更高的环境时,连接字符串为io.connect('https://domainName/')

时,同样无法正常工作

当我的应用尝试连接时,我收到以下错误: websocket.js:112 WebSocket connection to 'wss://domainName/socket.io/?EIO=3&transport=websocket' failed: Connection closed before receiving a handshake response

我错过了什么吗?

当我尝试通过socket.io测试仪点击服务器(更高版本的env)时,我得到的响应为No sockets Found,然后立即An error occurred while reconnecting

请注意:。服务器实例在较高环境中的api网关中注册。

Server.js

const app = require('express')()
const port = process.env.PORT || xxx5
const server = app.listen(port)
const io = require('socket.io').listen(server)
let ns = io.of('/namespace')

ns.on('connection', (socket) => {
logger.info('User connected')
socket.emit('messages', {new:2,[{title:'Hi'},{title:'Test'}]})
})

Client.js

import io from 'socket.io-client'
const socket = io(`https://domainName/namespace`, { transports:
['websocket'], rejectUnauthorized: true})
// const socket = io('http://localhost:xxx5/namespace', { transports:
['websocket'], rejectUnauthorized: true})
// above commented url is in local
socket.on('connect', () => {
logger.info("Connected")
})    
socket.on('messages', (data) => {
logger.info(`New messages : ${data.new}`)
})
socket.on('error', (error) => {
logger.error(`Error encountered : ${error}`)
})

1 个答案:

答案 0 :(得分:0)

您的应用程序是否以priveleged用户身份运行?考虑小于1024的任何TCP / UDP套接字和IANA保留端口,并且需要提升的priveleges作为监听服务进行绑定。

使用cURL也可以帮助提供必要的握手:(参见websocket握手过程的参考; https://en.m.wikipedia.org/wiki/WebSocket#Protocol_handshake

/Abc

相关问题