move connection state to rtmt

This commit is contained in:
Halit Aksoy 2022-09-04 01:06:07 +03:00
parent 886852611b
commit 472044577f
5 changed files with 25 additions and 5 deletions

View File

@ -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: "Bağlantısı Koptu",
ConnectingAgain: "Tekrar Bağlanılıyor",
ServerError: "Sunucu Hatası",
SearchingOpponet: "Rakip Aranıyor",

View File

@ -1 +0,0 @@
export type ConnectionState = "connecting" | "error" | "connected" | "reconnecting";

View File

@ -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);

View File

@ -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);
}
}

View File

@ -1,10 +1,20 @@
import { Context } from "../context/context";
import notyf from "./Notyf";
export default class Util {
public static range(size: number) {
var ans : number[] = [];
var ans: number[] = [];
for (let i = 0; i < size; i++) {
ans.push(i);
}
return ans;
}
public static checkConnectionAndMaybeAlert(context: Context): boolean {
if (context.rtmt.connectionState !== "connected") {
notyf.error(context.texts.ConnectionLost);
return true;
}
return false;
}
}