为什么 websocket 在使用代码 1000 的 react native 打开后立即关闭。(iOS)
Why does websocket close immediately after open in react native with code 1000. (iOS)
当我建立连接时,网络套接字立即关闭并显示代码 1000。有人能告诉我为什么会这样吗?
这是我的简单代码:
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2")
ws.onopen = () => console.log('OPEN')
ws.onclose = (e) => console.log('CLOSE: code: ' + e.code)
ws.onmessage = (e) => console.log('MESSAGE: ', e.data)
ws.onerror = () => console.log('ERROR')
// The result is:
// OPEN
// CLOSE: code: 1000
React原生信息
$ react-native info
Environment:
OS: macOS High Sierra 10.13.5
Node: 8.9.4
Yarn: 0.21.3
npm: 5.7.1
Watchman: 4.7.0
Xcode: Xcode 9.4.1 Build version 9F2000
Android Studio: 3.1 AI-173.4720617
Packages: (wanted => installed)
react: 16.3.1 => 16.3.1
react-native: 0.55.4 => 0.55.4
如果客户端发送headers不完全,一些服务器可以自动关闭连接。
WebSocket 的构造函数有第三个未记录的参数,可用于添加一些 headers。
我刚刚添加了额外的 headers 并且没有断开连接了。
// In this case server doesn't close the connection.
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2", null, {
headers: {
'Accept-Language': 'en,en-US;q=0.9,ru;q=0.8,de;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}
})
// In this case the server closes the connection.
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2")
当我建立连接时,网络套接字立即关闭并显示代码 1000。有人能告诉我为什么会这样吗?
这是我的简单代码:
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2")
ws.onopen = () => console.log('OPEN')
ws.onclose = (e) => console.log('CLOSE: code: ' + e.code)
ws.onmessage = (e) => console.log('MESSAGE: ', e.data)
ws.onerror = () => console.log('ERROR')
// The result is:
// OPEN
// CLOSE: code: 1000
React原生信息
$ react-native info
Environment:
OS: macOS High Sierra 10.13.5
Node: 8.9.4
Yarn: 0.21.3
npm: 5.7.1
Watchman: 4.7.0
Xcode: Xcode 9.4.1 Build version 9F2000
Android Studio: 3.1 AI-173.4720617
Packages: (wanted => installed)
react: 16.3.1 => 16.3.1
react-native: 0.55.4 => 0.55.4
如果客户端发送headers不完全,一些服务器可以自动关闭连接。 WebSocket 的构造函数有第三个未记录的参数,可用于添加一些 headers。 我刚刚添加了额外的 headers 并且没有断开连接了。
// In this case server doesn't close the connection.
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2", null, {
headers: {
'Accept-Language': 'en,en-US;q=0.9,ru;q=0.8,de;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}
})
// In this case the server closes the connection.
let ws = new WebSocket("wss://myhost/my-path?param1=value1¶m2=value2")