如标题所示,我在建立和连接到服务器时遇到错误。我正在使用一个名为 simple-websocket 的 npm 库来实现这一点。
在我的服务器端,我使用的是 Node ,而对于我的前端,我使用 browserify 捆绑 Node 模块。我已经尝试更改端口并搜索了没有结果的修复程序。
这是我的 Node 代码:
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
如果它有用,这里是浏览器代码: http://www.filedropper.com/bundle_2
最佳答案
您正在创建的 simple-websocket/server
实例不监听加密/wss/ssl 连接,它监听标准的 websocket 连接。您应该尝试使用 ws://localhost:8181
(而不是 wss://...
)连接到它。
关于javascript - 建立连接时出错:尝试建立并连接到 websocket 服务器时抛出 net::ERR_SSL_PROTOCOL_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729030/