update server adress
This commit is contained in:
parent
78f4e1d701
commit
8739f1ba02
@ -1,69 +1,70 @@
|
||||
import { decode, encode } from "./encode_decode_message"
|
||||
import { Bytes, OnMessage, RTMT } from "./rtmt"
|
||||
import { context } from '../context';
|
||||
import { decode, encode } from "./encode_decode_message";
|
||||
import { Bytes, OnMessage, RTMT } from "./rtmt";
|
||||
import { server } from "../service/http_service";
|
||||
|
||||
export class RTMTWS implements RTMT {
|
||||
private messageChannels: Map<String, OnMessage>;
|
||||
private ws: WebSocket;
|
||||
|
||||
private messageChannels: Map<String, OnMessage>
|
||||
private ws: WebSocket
|
||||
constructor() {
|
||||
this.messageChannels = new Map<String, OnMessage>();
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.messageChannels = new Map<String, OnMessage>()
|
||||
initWebSocket(
|
||||
userKey: string,
|
||||
onopen: () => any,
|
||||
onClose: () => any,
|
||||
onError: (event: Event) => any
|
||||
) {
|
||||
const url = server.wsServerAdress + "?userKey=" + userKey;
|
||||
const ws = new WebSocket(url);
|
||||
ws.binaryType = "arraybuffer"; //for firefox
|
||||
ws.onopen = () => {
|
||||
console.info("(RTMT) ws has opened");
|
||||
this.ws = ws;
|
||||
onopen();
|
||||
};
|
||||
ws.onclose = () => {
|
||||
console.info("(RTMT) ws has closed");
|
||||
//this.ws = undefined
|
||||
onClose();
|
||||
};
|
||||
|
||||
ws.onmessage = (event: MessageEvent) => {
|
||||
this.onWebSocketMessage(this, event);
|
||||
};
|
||||
|
||||
ws.addEventListener("error", (ev) => {
|
||||
console.error({ ws_error: ev });
|
||||
onError(ev);
|
||||
});
|
||||
}
|
||||
|
||||
sendMessage(channel: string, message: Object) {
|
||||
if (this.ws === undefined) {
|
||||
console.error("(RTMT) ws is undefined");
|
||||
return;
|
||||
}
|
||||
const data = encode(channel, message);
|
||||
this.ws.send(data);
|
||||
}
|
||||
|
||||
initWebSocket(userKey: string, onopen: () => any, onClose: () => any, onError: (event: Event) => any) {
|
||||
const url = server.wsServerAdress + '?userKey=' + userKey
|
||||
const ws = new WebSocket(url)
|
||||
ws.binaryType = "arraybuffer"; //for firefox
|
||||
ws.onopen = () => {
|
||||
console.log('(RTMT) ws has opened')
|
||||
this.ws = ws
|
||||
onopen()
|
||||
}
|
||||
ws.onclose = () => {
|
||||
console.log('(RTMT) ws has closed')
|
||||
//this.ws = undefined
|
||||
onClose()
|
||||
}
|
||||
listenMessage(channel: string, callback: OnMessage) {
|
||||
this.messageChannels.set(channel, callback);
|
||||
}
|
||||
|
||||
ws.onmessage = (event: MessageEvent) => {
|
||||
this.onWebSocketMessage(this, event)
|
||||
}
|
||||
onWebSocketMessage(rtmt: RTMTWS, event: MessageEvent) {
|
||||
const { channel, message } = decode(event.data);
|
||||
rtmt.onMessage(channel, message);
|
||||
}
|
||||
|
||||
ws.addEventListener("error", ev => {
|
||||
console.log({ ws_error: ev });
|
||||
onError(ev)
|
||||
})
|
||||
}
|
||||
|
||||
sendMessage(channel: string, message: Object) {
|
||||
console.log("(RTMT) Sending message to channel " + channel);
|
||||
if (this.ws === undefined) {
|
||||
console.log('(RTMT) ws is undefined')
|
||||
return
|
||||
}
|
||||
const data = encode(channel, message)
|
||||
this.ws.send(data)
|
||||
}
|
||||
|
||||
listenMessage(channel: string, callback: OnMessage) {
|
||||
this.messageChannels.set(channel, callback)
|
||||
}
|
||||
|
||||
onWebSocketMessage(rtmt: RTMTWS, event: MessageEvent) {
|
||||
const { channel, message } = decode(event.data)
|
||||
console.log("(RTMT) " + { channel, message });
|
||||
rtmt.onMessage(channel, message)
|
||||
}
|
||||
|
||||
onMessage(channel: string, message: Bytes) {
|
||||
const callback = this.messageChannels.get(channel)
|
||||
|
||||
if (callback) {
|
||||
callback(message)
|
||||
} else {
|
||||
console.log("(RTMT) Channel callback not found!" + channel);
|
||||
}
|
||||
onMessage(channel: string, message: Bytes) {
|
||||
const callback = this.messageChannels.get(channel);
|
||||
|
||||
if (callback) {
|
||||
callback(message);
|
||||
} else {
|
||||
console.warn("(RTMT) Channel callback not found!" + channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,33 +1,30 @@
|
||||
export type Server = {
|
||||
serverAdress: string
|
||||
wsServerAdress: string
|
||||
}
|
||||
serverAdress: string;
|
||||
wsServerAdress: string;
|
||||
};
|
||||
|
||||
export const server: Server = {
|
||||
serverAdress: "https://segin.one/mancala-backend",
|
||||
wsServerAdress: "wss://segin.one/mancala-backend/",
|
||||
}
|
||||
serverAdress: "https://segin.one/mancala-backend-beta",
|
||||
wsServerAdress: "wss://segin.one/mancala-backend-beta/",
|
||||
};
|
||||
|
||||
const useLocal = false
|
||||
const useLocal = false;
|
||||
|
||||
if (useLocal) {
|
||||
server.serverAdress = "http://localhost:5000"
|
||||
server.wsServerAdress = "ws://localhost:5000"
|
||||
server.serverAdress = "http://localhost:5000";
|
||||
server.wsServerAdress = "ws://localhost:5000";
|
||||
}
|
||||
|
||||
|
||||
interface HttpService {
|
||||
get: (route: string, succes: () => any, error: () => any) => any
|
||||
get: (route: string, succes: () => any, error: () => any) => any;
|
||||
}
|
||||
|
||||
class HttpServiceImpl implements HttpService {
|
||||
public serverAdress: string
|
||||
public serverAdress: string;
|
||||
|
||||
constructor(serverAdress: string) {
|
||||
this.serverAdress = serverAdress
|
||||
}
|
||||
constructor(serverAdress: string) {
|
||||
this.serverAdress = serverAdress;
|
||||
}
|
||||
|
||||
public get(route: string, succes: () => any, error: () => any): any {
|
||||
|
||||
}
|
||||
public get(route: string, succes: () => any, error: () => any): any {}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user