Commit 6d6df46d authored by Chunchi Che's avatar Chunchi Che

add socket middleware

parent 55a8cd22
Pipeline #17244 passed with stage
in 1 minute and 16 seconds
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import App from "./ui/App";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import { store } from "./store";
......
import { ygopro } from "../api/idl/ocgcore";
export enum socketCmd {
CONNECT,
DISCONNECT,
SEND,
}
export interface socketAction {
cmd: socketCmd;
ip?: string;
payload?: ygopro.YgoCtosMsg;
}
let ws: WebSocket | null = null;
export default function (action: socketAction) {
switch (action.cmd) {
case socketCmd.CONNECT: {
const ip = action.ip;
if (ip) {
ws = new WebSocket("ws://" + ip);
ws.onopen = () => {
console.log("WebSocket open.");
};
ws.onclose = () => {
console.log("WebSocket closed.");
ws = null;
};
ws.onmessage = (e) => {
const pb = ygopro.YgoStocMsg.deserializeBinary(e.data);
// todo
};
}
break;
}
case socketCmd.DISCONNECT: {
if (ws) {
ws.close();
}
break;
}
case socketCmd.SEND: {
const pb = action.payload;
if (ws && pb) {
ws.send(pb.serialize());
}
break;
}
default: {
console.log("Unhandled socket command: " + action.cmd);
break;
}
}
}
import React from "react";
import JoinRoom from "./ui/JoinRoom";
import WaitRoom from "./ui//WaitRoom";
import ThreeJs from "./ThreeJs";
import JoinRoom from "./JoinRoom";
import WaitRoom from "./WaitRoom";
import ThreeJs from "../ThreeJs";
import { Routes, Route } from "react-router-dom";
function App() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment