连接建立错误:尝试建立并连接到websocket服务器时抛出net :: ERR
如标题所示,我在建立和连接服务器时遇到错误。我正在使用一个名为simple-websocket的npm库来实现这一目标。
在我的服务器端,我使用节点,对于我的前端,我使用browserify捆绑节点模块。我已经尝试更改端口并搜索了没有结果的修复程序。
这是我的节点代码:
const Server = require('simple-websocket/server')
const server = new Server({port: 8181})
new Promise((resolve, reject) => {
server.on('connection', function(socket, request) {
resolve({socket, request})
})
}).then(data => {
console.log(data)
})
这是我的网页代码:
const Socket = require('simple-websocket')
exports.test = function() {
const socket = new Socket('wss://localhost:8181')
socket.on('connect', function() {
console.log('yes')
})
}
这是完整的错误:
WebSocket connection to 'wss://localhost:8181/' failed: Error in connection
establishment: net::ERR_SSL_PROTOCOL_ERROR
bundle.js:6106 Uncaught Error: connection error to wss://localhost:8181
at WebSocket.Socket.self._ws.onerror (bundle.js:6106)
Socket.self._ws.onerror @ bundle.js:6106
error (async)
Socket @ bundle.js:6105
exports.test @ bundle.js:2841
9../middleware-comms/Listen @ bundle.js:2836
o @ bundle.js:1
r @ bundle.js:1
(anonymous) @ bundle.js:1
如果它有用,这是浏览器化的代码:
回答如下:您正在创建的simple-websocket/server
实例不是在监听加密/ wss / ssl连接,它正在侦听标准的websocket连接。您应该尝试使用ws://localhost:8181
(而不是wss://...
)连接到它。