Commit af753793 authored by Chunchi Che's avatar Chunchi Che Committed by GitHub

Merge pull request #8 from DarkNeos/dev

Dev
parents bcbb998f a4c9a8a7
...@@ -29,17 +29,17 @@ export default function JoinRoom() { ...@@ -29,17 +29,17 @@ export default function JoinRoom() {
<p> <p>
<input <input
type="text" type="text"
title="passwd" title="ip"
value={passWd} value={ip}
onChange={handlePasswdChange} onChange={handleIpChange}
></input> ></input>
</p> </p>
<p> <p>
<input <input
type="text" type="text"
title="ip" title="passwd"
value={ip} value={passWd}
onChange={handleIpChange} onChange={handlePasswdChange}
></input> ></input>
</p> </p>
<button> <button>
......
import React, { useRef, useEffect } from "react"; import React, { useRef, useEffect, useState } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { ygopro } from "./api/idl/ocgcore"; import { ygopro } from "./api/idl/ocgcore";
...@@ -9,6 +9,9 @@ export default function WaitRoom() { ...@@ -9,6 +9,9 @@ export default function WaitRoom() {
ip?: string; ip?: string;
}>(); }>();
const [joined, setJoined] = useState<string>("false");
const [chat, setChat] = useState<string>("");
const ws = useRef<WebSocket | null>(null); const ws = useRef<WebSocket | null>(null);
const { player, passWd, ip } = params; const { player, passWd, ip } = params;
...@@ -29,23 +32,10 @@ export default function WaitRoom() { ...@@ -29,23 +32,10 @@ export default function WaitRoom() {
) { ) {
const wsCurrent = ws.current; const wsCurrent = ws.current;
const playerInfo = new ygopro.YgoCtosMsg({ wsCurrent.binaryType = "arraybuffer";
ctos_player_info: new ygopro.CtosPlayerInfo({
name: player
})
});
wsCurrent.send(playerInfo.serialize());
const joinGame = new ygopro.YgoCtosMsg({ sendPlayerInfo(wsCurrent, player);
ctos_join_game: new ygopro.CtosJoinGame({ sendJoinGame(wsCurrent, 4947, passWd);
version: 4947, // todo: use config
gameid: 0,
passwd: passWd
})
});
wsCurrent.send(joinGame.serialize());
} }
}; };
...@@ -54,7 +44,27 @@ export default function WaitRoom() { ...@@ -54,7 +44,27 @@ export default function WaitRoom() {
}; };
ws.current.onmessage = e => { ws.current.onmessage = e => {
console.log("websocket read message: " + e.data); const pb = ygopro.YgoStocMsg.deserializeBinary(e.data);
switch (pb.msg) {
case "stoc_join_game": {
const msg = pb.stoc_join_game;
console.log("joinGame msg=" + msg);
setJoined("true");
break;
}
case "stoc_chat": {
const chat = pb.stoc_chat;
setChat(chat.msg);
break;
}
default: {
break;
}
}
}; };
const wsCurrent = ws.current; const wsCurrent = ws.current;
...@@ -68,9 +78,30 @@ export default function WaitRoom() { ...@@ -68,9 +78,30 @@ export default function WaitRoom() {
return ( return (
<div> <div>
<p>player: {params.player}</p> <p>joined: {joined}</p>
<p>passwd: {params.passWd}</p> <p>chat: {chat}</p>
<p>ip: {params.ip}</p>
</div> </div>
); );
} }
function sendPlayerInfo(ws: WebSocket, player: string) {
const playerInfo = new ygopro.YgoCtosMsg({
ctos_player_info: new ygopro.CtosPlayerInfo({
name: player
})
});
ws.send(playerInfo.serialize());
}
function sendJoinGame(ws: WebSocket, version: number, passWd: string) {
const joinGame = new ygopro.YgoCtosMsg({
ctos_join_game: new ygopro.CtosJoinGame({
version, // todo: use config
gameid: 0,
passwd: passWd
})
});
ws.send(joinGame.serialize());
}
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