move connection state to rtmt
This commit is contained in:
parent
886852611b
commit
472044577f
@ -51,7 +51,7 @@ export const EnUs: Texts = {
|
||||
Connecting: "Connecting",
|
||||
Connected: "Connected",
|
||||
CannotConnect: "Can't Connect",
|
||||
ConnectionLost: "Connection Lost",
|
||||
ConnectionLost: "Network Connection Lost",
|
||||
ConnectingAgain: "Connecting Again",
|
||||
ServerError: "Server Error",
|
||||
SearchingOpponet: "Searching Opponet",
|
||||
@ -89,7 +89,7 @@ export const TrTr: Texts = {
|
||||
Connecting: "Bağlanılıyor",
|
||||
Connected: "Bağlandı",
|
||||
CannotConnect: "Bağlanılamadı",
|
||||
ConnectionLost: "Bağlantı Koptu",
|
||||
ConnectionLost: "Ağ Bağlantısı Koptu",
|
||||
ConnectingAgain: "Tekrar Bağlanılıyor",
|
||||
ServerError: "Sunucu Hatası",
|
||||
SearchingOpponet: "Rakip Aranıyor",
|
||||
|
||||
@ -1 +0,0 @@
|
||||
export type ConnectionState = "connecting" | "error" | "connected" | "reconnecting";
|
||||
@ -3,10 +3,12 @@ import EventEmitter2, { Listener } from "eventemitter2"
|
||||
export type Bytes = Uint8Array
|
||||
export type OnMessage = (message : Object) => any
|
||||
|
||||
export type ConnectionState = "none" | "connecting" | "error" | "connected" | "closed" | "reconnecting";
|
||||
|
||||
export type RtmtEventTypes = "open" | "close" | "connected" | "error" | "disconnected" | "message";
|
||||
|
||||
export interface RTMT extends EventEmitter2 {
|
||||
get connectionState() : ConnectionState;
|
||||
sendMessage: (channel: string, message: Object) => void;
|
||||
addMessageListener(channel: string, callback: (message: any) => void);
|
||||
removeMessageListener(channel: string, callback: (message: any) => void);
|
||||
|
||||
@ -2,7 +2,7 @@ import { decode, encode } from "./encode_decode_message";
|
||||
import { channel_ping, channel_pong } from "../const/channel_names";
|
||||
import { server } from "../const/config";
|
||||
import EventEmitter2, { Listener } from "eventemitter2";
|
||||
import { Bytes, RTMT, RtmtEventTypes } from "./rtmt";
|
||||
import { Bytes, ConnectionState, RTMT, RtmtEventTypes } from "./rtmt";
|
||||
|
||||
const PING_INTERVAL = 15000, PING_INTERVAL_BUFFER_TIME = 1000;
|
||||
const MESSAGE_CHANNEL_PREFIX = "message_channel";
|
||||
@ -10,24 +10,32 @@ const MESSAGE_CHANNEL_PREFIX = "message_channel";
|
||||
export class RTMTWS extends EventEmitter2 implements RTMT {
|
||||
private webSocket: WebSocket;
|
||||
private pingTimeout?: number = undefined;
|
||||
private _connectionState: ConnectionState = "none";
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
get connectionState(): ConnectionState {
|
||||
return this._connectionState;
|
||||
}
|
||||
|
||||
public initWebSocket(userKey: string) {
|
||||
this._connectionState = this._connectionState !== "none" ? "reconnecting" : "connecting";
|
||||
const url = server.wsServerAdress + "?userKey=" + userKey;
|
||||
const webSocket = new WebSocket(url);
|
||||
webSocket.onopen = () => {
|
||||
console.info("(RTMT) WebSocket has opened");
|
||||
this.webSocket = webSocket;
|
||||
this.heartbeat();
|
||||
this._connectionState = "connected";
|
||||
this.emit("open");
|
||||
};
|
||||
webSocket.onclose = () => {
|
||||
console.info("(RTMT) WebSocket has closed");
|
||||
//this.WebSocket = undefined
|
||||
clearTimeout(this.pingTimeout);
|
||||
this._connectionState = "closed";
|
||||
this.emit("close");
|
||||
};
|
||||
webSocket.onmessage = (event: MessageEvent) => {
|
||||
@ -36,6 +44,7 @@ export class RTMTWS extends EventEmitter2 implements RTMT {
|
||||
};
|
||||
webSocket.onerror = (error) => {
|
||||
console.error(error);
|
||||
this._connectionState = "error";
|
||||
this.emit("error", error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { Context } from "../context/context";
|
||||
import notyf from "./Notyf";
|
||||
|
||||
export default class Util {
|
||||
public static range(size: number) {
|
||||
@ -7,4 +9,12 @@ export default class Util {
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static checkConnectionAndMaybeAlert(context: Context): boolean {
|
||||
if (context.rtmt.connectionState !== "connected") {
|
||||
notyf.error(context.texts.ConnectionLost);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user