Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
love_飞影
Neos
Commits
05509159
Commit
05509159
authored
Oct 18, 2022
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish websocket middleware
parent
53511cbc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
34 deletions
+49
-34
src/middleware/socket.ts
src/middleware/socket.ts
+40
-6
src/ui/WaitRoom.tsx
src/ui/WaitRoom.tsx
+9
-28
No files found.
src/middleware/socket.ts
View file @
05509159
import
{
ygopro
}
from
"
../api/idl/ocgcore
"
;
import
{
ygopro
}
from
"
../api/idl/ocgcore
"
;
import
{
setJoined
,
selectJoined
}
from
"
../reducers/joinSlice
"
;
import
{
setJoined
}
from
"
../reducers/joinSlice
"
;
import
{
postChat
,
selectChat
}
from
"
../reducers/chatSlice
"
;
import
{
postChat
}
from
"
../reducers/chatSlice
"
;
import
{
useAppDispatch
,
useAppSelector
}
from
"
../hook
"
;
import
{
store
}
from
"
../store
"
;
import
{
import
{
player0Enter
,
player0Enter
,
player1Enter
,
player1Enter
,
...
@@ -24,6 +24,9 @@ export enum socketCmd {
...
@@ -24,6 +24,9 @@ export enum socketCmd {
export
interface
socketAction
{
export
interface
socketAction
{
cmd
:
socketCmd
;
cmd
:
socketCmd
;
ip
?:
string
;
ip
?:
string
;
player
?:
string
;
version
?:
number
;
passWd
?:
string
;
payload
?:
ygopro
.
YgoCtosMsg
;
payload
?:
ygopro
.
YgoCtosMsg
;
}
}
...
@@ -36,13 +39,21 @@ export default function (action: socketAction) {
...
@@ -36,13 +39,21 @@ export default function (action: socketAction) {
switch
(
action
.
cmd
)
{
switch
(
action
.
cmd
)
{
case
socketCmd
.
CONNECT
:
{
case
socketCmd
.
CONNECT
:
{
const
ip
=
action
.
ip
;
const
ip
=
action
.
ip
;
if
(
ip
)
{
const
player
=
action
.
player
;
const
version
=
action
.
version
;
const
passWd
=
action
.
passWd
;
if
(
ip
&&
player
&&
version
&&
passWd
)
{
ws
=
new
WebSocket
(
"
ws://
"
+
ip
);
ws
=
new
WebSocket
(
"
ws://
"
+
ip
);
ws
.
onopen
=
()
=>
{
ws
.
onopen
=
()
=>
{
console
.
log
(
"
WebSocket open.
"
);
console
.
log
(
"
WebSocket open.
"
);
ws
!
.
binaryType
=
"
arraybuffer
"
;
if
(
ws
&&
ws
.
readyState
==
1
)
{
ws
.
binaryType
=
"
arraybuffer
"
;
sendPlayerInfo
(
ws
,
player
);
sendJoinGame
(
ws
,
version
,
passWd
);
}
};
};
ws
.
onclose
=
()
=>
{
ws
.
onclose
=
()
=>
{
console
.
log
(
"
WebSocket closed.
"
);
console
.
log
(
"
WebSocket closed.
"
);
...
@@ -50,7 +61,7 @@ export default function (action: socketAction) {
...
@@ -50,7 +61,7 @@ export default function (action: socketAction) {
};
};
ws
.
onmessage
=
(
e
)
=>
{
ws
.
onmessage
=
(
e
)
=>
{
const
pb
=
ygopro
.
YgoStocMsg
.
deserializeBinary
(
e
.
data
);
const
pb
=
ygopro
.
YgoStocMsg
.
deserializeBinary
(
e
.
data
);
const
dispatch
=
useAppDispatch
()
;
const
dispatch
=
store
.
dispatch
;
switch
(
pb
.
msg
)
{
switch
(
pb
.
msg
)
{
case
"
stoc_join_game
"
:
{
case
"
stoc_join_game
"
:
{
...
@@ -210,3 +221,26 @@ export default function (action: socketAction) {
...
@@ -210,3 +221,26 @@ export default function (action: socketAction) {
}
}
}
}
}
}
// todo: move to api/*
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
());
}
src/ui/WaitRoom.tsx
View file @
05509159
import
React
,
{
use
Ref
,
useEffect
,
useState
,
useReducer
}
from
"
react
"
;
import
React
,
{
use
Effect
,
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
"
;
import
{
fetchDeck
,
IDeck
}
from
"
../api/Card
"
;
import
{
fetchDeck
,
IDeck
}
from
"
../api/Card
"
;
...
@@ -16,7 +16,6 @@ import {
...
@@ -16,7 +16,6 @@ import {
import
socketMiddleWare
,
{
socketCmd
}
from
"
../middleware/socket
"
;
import
socketMiddleWare
,
{
socketCmd
}
from
"
../middleware/socket
"
;
const
READY_STATE
=
"
ready
"
;
const
READY_STATE
=
"
ready
"
;
const
NO_READY_STATE
=
"
not ready
"
;
export
default
function
WaitRoom
()
{
export
default
function
WaitRoom
()
{
const
params
=
useParams
<
{
const
params
=
useParams
<
{
...
@@ -35,10 +34,13 @@ export default function WaitRoom() {
...
@@ -35,10 +34,13 @@ export default function WaitRoom() {
passWd
!=
null
&&
passWd
!=
null
&&
passWd
.
length
!=
0
passWd
.
length
!=
0
)
{
)
{
socketMiddleWare
({
cmd
:
socketCmd
.
CONNECT
,
ip
});
socketMiddleWare
({
cmd
:
socketCmd
.
CONNECT
,
sendPlayerInfo
(
player
);
ip
,
sendJoinGame
(
4947
,
passWd
);
player
,
version
:
4947
,
passWd
,
});
}
}
},
[]);
},
[]);
...
@@ -113,28 +115,7 @@ export default function WaitRoom() {
...
@@ -113,28 +115,7 @@ export default function WaitRoom() {
);
);
}
}
function
sendPlayerInfo
(
player
:
string
)
{
// todo: move to api/*
const
playerInfo
=
new
ygopro
.
YgoCtosMsg
({
ctos_player_info
:
new
ygopro
.
CtosPlayerInfo
({
name
:
player
,
}),
});
socketMiddleWare
({
cmd
:
socketCmd
.
SEND
,
payload
:
playerInfo
});
}
function
sendJoinGame
(
version
:
number
,
passWd
:
string
)
{
const
joinGame
=
new
ygopro
.
YgoCtosMsg
({
ctos_join_game
:
new
ygopro
.
CtosJoinGame
({
version
,
// todo: use config
gameid
:
0
,
passwd
:
passWd
,
}),
});
socketMiddleWare
({
cmd
:
socketCmd
.
SEND
,
payload
:
joinGame
});
}
function
sendUpdateDeck
(
deck
:
IDeck
)
{
function
sendUpdateDeck
(
deck
:
IDeck
)
{
const
updateDeck
=
new
ygopro
.
YgoCtosMsg
({
const
updateDeck
=
new
ygopro
.
YgoCtosMsg
({
ctos_update_deck
:
new
ygopro
.
CtosUpdateDeck
({
ctos_update_deck
:
new
ygopro
.
CtosUpdateDeck
({
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment