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
c4303824
Commit
c4303824
authored
May 19, 2023
by
chechunchi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add src/infra/stream.ts
parent
2869c08b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
91 additions
and
14 deletions
+91
-14
src/infra/index.ts
src/infra/index.ts
+4
-0
src/infra/sleep.ts
src/infra/sleep.ts
+2
-0
src/infra/stream.ts
src/infra/stream.ts
+74
-0
src/middleware/socket.ts
src/middleware/socket.ts
+9
-12
src/service/onSocketMessage.ts
src/service/onSocketMessage.ts
+1
-1
src/service/onSocketOpen.ts
src/service/onSocketOpen.ts
+1
-1
No files found.
src/infra/index.ts
0 → 100644
View file @
c4303824
// Some implementation of infrastructure
export
*
from
"
./sleep
"
;
export
*
from
"
./stream
"
;
src/infra/sleep.ts
0 → 100644
View file @
c4303824
export
const
sleep
=
(
delay
:
number
)
=>
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
delay
));
src/infra/stream.ts
0 → 100644
View file @
c4303824
import
handleSocketMessage
from
"
@/service/onSocketMessage
"
;
import
{
sleep
}
from
"
./sleep
"
;
const
SLEEP_INTERVAL
=
200
;
export
class
WebSocketStream
{
public
ws
:
WebSocket
;
stream
:
ReadableStream
;
constructor
(
ip
:
string
,
onWsOpen
?:
(
ws
:
WebSocket
,
ev
:
Event
)
=>
any
)
{
this
.
ws
=
new
WebSocket
(
"
wss://
"
+
ip
);
if
(
onWsOpen
)
{
this
.
ws
.
onopen
=
(
e
)
=>
onWsOpen
(
this
.
ws
,
e
);
}
const
ws
=
this
.
ws
;
this
.
stream
=
new
ReadableStream
({
start
(
controller
)
{
// 当Websocket有数据到达时,加入队列
ws
.
onmessage
=
(
event
)
=>
{
controller
.
enqueue
(
event
);
};
ws
.
onclose
=
()
=>
{
console
.
info
(
"
Websocket closed.
"
);
controller
.
close
();
};
},
pull
(
_
)
{
// currently not really need
},
cancel
()
{
// currently not
},
});
}
// 异步地从Websocket中获取数据并处理
async
execute
(
onMessage
:
(
event
:
MessageEvent
)
=>
Promise
<
void
>
)
{
const
reader
:
ReadableStreamDefaultReader
<
MessageEvent
>
=
this
.
stream
.
getReader
();
const
ws
=
this
.
ws
;
reader
.
read
().
then
(
async
function
process
({
done
,
value
}):
Promise
<
void
>
{
if
(
done
)
{
if
(
ws
.
readyState
==
WebSocket
.
CLOSED
)
{
// websocket connection has been closed
console
.
info
(
"
WebSocket closed, stream complete.
"
);
return
;
}
else
{
// websocket not closed, sleep sometime, wait for next message from server
await
sleep
(
SLEEP_INTERVAL
);
return
reader
.
read
().
then
(
process
);
}
}
if
(
value
)
{
await
onMessage
(
value
);
}
else
{
console
.
warn
(
"
value from ReadableStream is undefined!
"
);
}
// read some more, and call process function again
return
reader
.
read
().
then
(
process
);
});
}
// 关闭流
close
()
{
this
.
ws
.
close
();
}
}
src/middleware/socket.ts
View file @
c4303824
...
@@ -4,6 +4,8 @@
...
@@ -4,6 +4,8 @@
* 所有长连接/Websocket相关的逻辑都应该收敛在这里。
* 所有长连接/Websocket相关的逻辑都应该收敛在这里。
*
*
* */
* */
import
{
WebSocketStream
}
from
"
@/infra
"
;
import
handleSocketMessage
from
"
../service/onSocketMessage
"
;
import
handleSocketMessage
from
"
../service/onSocketMessage
"
;
import
handleSocketOpen
from
"
../service/onSocketOpen
"
;
import
handleSocketOpen
from
"
../service/onSocketOpen
"
;
...
@@ -28,24 +30,19 @@ export interface socketAction {
...
@@ -28,24 +30,19 @@ export interface socketAction {
payload
?:
Uint8Array
;
payload
?:
Uint8Array
;
}
}
let
ws
:
WebSocket
|
null
=
null
;
let
ws
:
WebSocket
Stream
|
null
=
null
;
// FIXME: 应该有个返回值,告诉业务方本次请求的结果。比如建立长连接失败。
// FIXME: 应该有个返回值,告诉业务方本次请求的结果。比如建立长连接失败。
export
default
function
(
action
:
socketAction
)
{
export
default
async
function
(
action
:
socketAction
)
{
switch
(
action
.
cmd
)
{
switch
(
action
.
cmd
)
{
case
socketCmd
.
CONNECT
:
{
case
socketCmd
.
CONNECT
:
{
const
info
=
action
.
initInfo
;
const
info
=
action
.
initInfo
;
if
(
info
)
{
if
(
info
)
{
ws
=
new
WebSocket
(
"
wss://
"
+
info
.
ip
);
ws
=
new
WebSocketStream
(
info
.
ip
,
(
conn
,
_event
)
=>
handleSocketOpen
(
conn
,
info
.
ip
,
info
.
player
,
info
.
passWd
)
);
ws
.
onopen
=
()
=>
{
await
ws
.
execute
(
handleSocketMessage
);
handleSocketOpen
(
ws
,
info
.
ip
,
info
.
player
,
info
.
passWd
);
};
ws
.
onclose
=
()
=>
{
console
.
log
(
"
WebSocket closed.
"
);
ws
=
null
;
};
ws
.
onmessage
=
handleSocketMessage
;
}
}
break
;
break
;
...
@@ -60,7 +57,7 @@ export default function (action: socketAction) {
...
@@ -60,7 +57,7 @@ export default function (action: socketAction) {
case
socketCmd
.
SEND
:
{
case
socketCmd
.
SEND
:
{
const
payload
=
action
.
payload
;
const
payload
=
action
.
payload
;
if
(
ws
&&
payload
)
{
if
(
ws
&&
payload
)
{
ws
.
send
(
payload
);
ws
.
ws
.
send
(
payload
);
}
}
break
;
break
;
...
...
src/service/onSocketMessage.ts
View file @
c4303824
...
@@ -28,7 +28,7 @@ const NeosConfig = useConfig();
...
@@ -28,7 +28,7 @@ const NeosConfig = useConfig();
* 然后再分发到各个处理函数中去处理。
* 然后再分发到各个处理函数中去处理。
*
*
* */
* */
export
default
function
handleSocketMessage
(
e
:
MessageEvent
)
{
export
default
async
function
handleSocketMessage
(
e
:
MessageEvent
)
{
const
packet
=
YgoProPacket
.
deserialize
(
e
.
data
);
const
packet
=
YgoProPacket
.
deserialize
(
e
.
data
);
const
pb
=
adaptStoc
(
packet
);
const
pb
=
adaptStoc
(
packet
);
const
delay
=
handleDelay
(
pb
);
const
delay
=
handleDelay
(
pb
);
...
...
src/service/onSocketOpen.ts
View file @
c4303824
...
@@ -12,7 +12,7 @@ const NeosConfig = useConfig();
...
@@ -12,7 +12,7 @@ const NeosConfig = useConfig();
*
*
* */
* */
export
default
function
handleSocketOpen
(
export
default
function
handleSocketOpen
(
ws
:
WebSocket
|
null
,
ws
:
WebSocket
|
undefined
,
_ip
:
string
,
_ip
:
string
,
player
:
string
,
player
:
string
,
passWd
:
string
passWd
:
string
...
...
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