Warn/throw on unexpected attach addon socket state
This commit is contained in:
@@ -47,16 +47,14 @@ export class AttachAddon implements ITerminalAddon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _sendData(data: string): void {
|
private _sendData(data: string): void {
|
||||||
// TODO: do something better than just swallowing
|
if (!this._checkOpenSocket()) {
|
||||||
// the data if the socket is not in a working condition
|
|
||||||
if (this._socket.readyState !== 1) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._socket.send(data);
|
this._socket.send(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _sendBinary(data: string): void {
|
private _sendBinary(data: string): void {
|
||||||
if (this._socket.readyState !== 1) {
|
if (!this._checkOpenSocket()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const buffer = new Uint8Array(data.length);
|
const buffer = new Uint8Array(data.length);
|
||||||
@@ -65,6 +63,22 @@ export class AttachAddon implements ITerminalAddon {
|
|||||||
}
|
}
|
||||||
this._socket.send(buffer);
|
this._socket.send(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _checkOpenSocket(): boolean {
|
||||||
|
switch (this._socket.readyState) {
|
||||||
|
case WebSocket.OPEN:
|
||||||
|
return true;
|
||||||
|
case WebSocket.CONNECTING:
|
||||||
|
throw new Error('Attach addon was loaded before socket was open');
|
||||||
|
case WebSocket.CLOSING:
|
||||||
|
console.warn('Attach addon socket is closing');
|
||||||
|
return false;
|
||||||
|
case WebSocket.CLOSED:
|
||||||
|
throw new Error('Attach addon socket is closed');
|
||||||
|
default:
|
||||||
|
throw new Error('Unexpected socket state');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSocketListener<K extends keyof WebSocketEventMap>(socket: WebSocket, type: K, handler: (this: WebSocket, ev: WebSocketEventMap[K]) => any): IDisposable {
|
function addSocketListener<K extends keyof WebSocketEventMap>(socket: WebSocket, type: K, handler: (this: WebSocket, ev: WebSocketEventMap[K]) => any): IDisposable {
|
||||||
|
|||||||
Reference in New Issue
Block a user