We’re noticing points with Safari iOS not calling Websocket occasions when the Websocket connection is misplaced. Our net utility has no clue the Websocket’s connection has been misplaced. On Android
units, as quickly because the connection is severed, the shut and error Websocket occasions are fired.
We created a fast instance.
Websocket server in nodeJS
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 8080});
wss.on('connection', perform connection(ws) {
ws.on('message', perform incoming(message) {
ws.ship(`You despatched: ${message}`);
});
ws.on('shut', perform shut() {
console.log('Shopper has disconnected');
});
});
Easy consumer
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket Instance</title>
</head>
<physique>
<h1>WebSocket Instance</h1>
<div id="output"></div>
<type>
<label>
Message:
<enter sort="textual content" id="message">
</label>
<button sort="submit" id="ship">Ship</button>
</type>
<script>
const output = doc.getElementById('output');
const messageInput = doc.getElementById('message');
const sendButton = doc.getElementById('ship');
const ws = new WebSocket('ws://localhost:8080');
ws.addEventListener('open', perform (occasion) {
console.log((new Date()).toISOString(), '********************** OPEN **********************');
});
ws.addEventListener('shut', perform (occasion) {
console.log((new Date()).toISOString(), '********************** CLOSE **********************');
});
ws.addEventListener('error', perform (occasion) {
console.log((new Date()).toISOString(), '********************** ERROR **********************');
});
ws.addEventListener('message', perform (occasion) {
console.log((new Date()).toISOString(), '********************** MESSAGE **********************');
// Append the message to the output div
const message = doc.createElement('p');
message.textContent = occasion.knowledge;
output.appendChild(message);
});
sendButton.addEventListener('click on', perform (occasion) {
occasion.preventDefault();
const message = messageInput.worth;
// Ship the message to the server
ws.ship(message);
});
</script>
</physique>
</html>
When the above code runs, iOS cell Safari doesn’t hearth the occasions shut
or error
when the Websocket connection is closed.
Examples of closing the Websocket are:
- Placing gadget on airplane mode
- Powering off wifi router
- Turning gadget’s wifi off
As talked about earlier than this works tremendous on Android and different units, solely iOS Safari behaves this manner, has anybody ever encountered this with their Internet Functions?