Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
op_16
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
template admin
op_16
Commits
a690d2ac
Commit
a690d2ac
authored
Jun 16, 2022
by
范雪寒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:
parent
860b4b42
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
256 additions
and
37 deletions
+256
-37
NetworkHelper.ts
assets/op_16/script/NetworkHelper.ts
+49
-37
NetworkHelper2.ts
assets/op_16/script/NetworkHelper2.ts
+198
-0
NetworkHelper2.ts.meta
assets/op_16/script/NetworkHelper2.ts.meta
+9
-0
No files found.
assets/op_16/script/NetworkHelper.ts
View file @
a690d2ac
import
{
RandomInt
}
from
"
./util
"
;
export
class
NetworkHelper
{
_eventListeners
:
any
=
{};
ctor
()
{
}
...
...
@@ -27,79 +29,51 @@ export class NetworkHelper {
room
:
any
;
client
:
any
;
async
initRoom
()
{
this
.
client
=
new
(
<
any
>
(
window
)).
GOBE
.
Client
({
clientId
:
'
857970243220294656
'
,
// 客户端ID
clientSecret
:
'
489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D
'
,
// 客户端密钥
appId
:
'
105879509
'
,
// 应用的ID
openId
:
'
op_16
'
+
Math
.
random
(),
// 玩家ID
});
await
this
.
client
.
init
();
this
.
client
=
{};
}
player
:
any
;
async
joinRoom
(
roomType
:
string
,
maxPlayers
:
number
)
{
const
matchRoomConfig
=
{
matchParams
:
{
param1
:
'
param1
'
,
},
roomType
:
roomType
,
maxPlayers
:
maxPlayers
,
};
this
.
room
=
await
this
.
client
.
matchRoom
(
matchRoomConfig
);
const
playerId
=
RandomInt
(
10000000
);
this
.
room
=
new
FakeRoom
(
playerId
);
this
.
room
.
players
.
push
(
this
.
room
.
player
);
this
.
player
=
this
.
room
.
player
;
console
.
log
(
'
汪汪汪:
'
+
this
.
room
.
players
.
length
);
await
this
.
startFrameSync
();
console
.
log
(
'
汪汪汪2
'
);
this
.
room
.
onJoin
(
this
.
onJoinRoom
.
bind
(
this
));
this
.
room
.
onLeave
(
this
.
onLeaveRoom
.
bind
(
this
));
//
this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this
.
room
.
onStartFrameSync
(
this
.
onStartFrameSync
.
bind
(
this
));
this
.
room
.
onStopFrameSync
(
this
.
onStopFrameSync
.
bind
(
this
));
this
.
room
.
onRecvFrame
(
this
.
onRecvFrame
.
bind
(
this
));
this
.
room
.
onDisconnect
(
this
.
onDisconnect
.
bind
(
this
));
this
.
room
.
onDismiss
(
this
.
onDisconnect
.
bind
(
this
));
await
this
.
startFrameSync
();
return
this
.
player
.
playerId
;
}
async
leaveRoom
()
{
await
this
.
room
.
leave
();
console
.
log
(
"
退房成功
"
);
}
async
dismissRoom
()
{
if
(
this
.
room
.
ownerId
==
this
.
player
.
id
)
{
await
this
.
client
.
dismissRoom
();
console
.
log
(
"
房间已解散
"
);
}
}
async
closeRoom
()
{
await
this
.
client
.
cancelMatch
();
}
async
startFrameSync
()
{
await
this
.
room
.
startFrameSync
();
// return new Promise((resolve, reject) => {
// this.room.onStartFrameSync(() => {
// cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
// resolve(null);
// });
// });
console
.
log
(
"
开始帧同步成功
"
);
}
async
stopFrameSync
()
{
await
this
.
room
.
stopFrameSync
();
console
.
log
(
"
停止帧同步成功
"
);
}
sendFrame
(
data
:
any
)
{
console
.
warn
(
'
sendFrame:
'
+
JSON
.
stringify
(
data
));
if
(
this
.
room
.
_isSyncing
)
{
this
.
room
.
sendFrame
(
JSON
.
stringify
(
data
));
}
else
{
console
.
log
(
'
is syncing = false
'
);
this
.
onRecvFrame
({
data
});
}
this
.
onRecvFrame
({
data
});
}
onJoinRoom
(
event
)
{
...
...
@@ -196,3 +170,41 @@ export function asyncDelay(time) {
.
start
();
});
}
class
FakeRoom
{
ownerId
;
players
=
[];
player
:
any
;
constructor
(
playerId
)
{
this
.
player
=
{
playerId
:
playerId
};
this
.
ownerId
=
playerId
;
}
_onJoin
;
onJoin
(
func
:
Function
)
{
this
.
_onJoin
=
func
;
}
_onLeave
;
onLeave
(
func
:
Function
)
{
this
.
_onLeave
=
func
;
}
_onStartFrameSync
;
onStartFrameSync
(
func
:
Function
)
{
this
.
_onStartFrameSync
=
func
;
}
_onStopFrameSync
;
onStopFrameSync
(
func
:
Function
)
{
this
.
_onStopFrameSync
=
func
;
}
_onRecvFrame
;
onRecvFrame
(
func
:
Function
)
{
this
.
_onRecvFrame
=
func
;
}
_onDisconnect
;
onDisconnect
(
func
:
Function
)
{
this
.
_onDisconnect
=
func
;
}
_onDismiss
;
onDismiss
(
func
:
Function
)
{
this
.
_onDismiss
=
func
;
}
}
\ No newline at end of file
assets/op_16/script/NetworkHelper2.ts
0 → 100644
View file @
a690d2ac
export
class
NetworkHelper
{
_eventListeners
:
any
=
{};
ctor
()
{
}
on
(
eventName
,
func
)
{
this
.
_eventListeners
[
eventName
]
=
func
;
}
async
init
(
roomType
:
string
,
maxPlayers
:
number
)
{
await
this
.
initRoom
();
return
await
this
.
joinRoom
(
roomType
,
maxPlayers
);
}
async
startGame
()
{
await
this
.
startFrameSync
();
await
this
.
closeRoom
();
}
async
stopGame
()
{
await
this
.
stopFrameSync
();
await
this
.
closeRoom
();
await
this
.
leaveRoom
();
}
listenerInited
=
false
;
room
:
any
;
client
:
any
;
async
initRoom
()
{
this
.
client
=
new
(
<
any
>
(
window
)).
GOBE
.
Client
({
clientId
:
'
857970243220294656
'
,
// 客户端ID
clientSecret
:
'
489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D
'
,
// 客户端密钥
appId
:
'
105879509
'
,
// 应用的ID
openId
:
'
op_16
'
+
Math
.
random
(),
// 玩家ID
});
await
this
.
client
.
init
();
}
player
:
any
;
async
joinRoom
(
roomType
:
string
,
maxPlayers
:
number
)
{
const
matchRoomConfig
=
{
matchParams
:
{
param1
:
'
param1
'
,
},
roomType
:
roomType
,
maxPlayers
:
maxPlayers
,
};
this
.
room
=
await
this
.
client
.
matchRoom
(
matchRoomConfig
);
this
.
player
=
this
.
room
.
player
;
console
.
log
(
'
汪汪汪:
'
+
this
.
room
.
players
.
length
);
await
this
.
startFrameSync
();
console
.
log
(
'
汪汪汪2
'
);
this
.
room
.
onJoin
(
this
.
onJoinRoom
.
bind
(
this
));
this
.
room
.
onLeave
(
this
.
onLeaveRoom
.
bind
(
this
));
// this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this
.
room
.
onStopFrameSync
(
this
.
onStopFrameSync
.
bind
(
this
));
this
.
room
.
onRecvFrame
(
this
.
onRecvFrame
.
bind
(
this
));
this
.
room
.
onDisconnect
(
this
.
onDisconnect
.
bind
(
this
));
this
.
room
.
onDismiss
(
this
.
onDisconnect
.
bind
(
this
));
return
this
.
player
.
playerId
;
}
async
leaveRoom
()
{
await
this
.
room
.
leave
();
console
.
log
(
"
退房成功
"
);
}
async
dismissRoom
()
{
if
(
this
.
room
.
ownerId
==
this
.
player
.
id
)
{
await
this
.
client
.
dismissRoom
();
console
.
log
(
"
房间已解散
"
);
}
}
async
closeRoom
()
{
await
this
.
client
.
cancelMatch
();
}
async
startFrameSync
()
{
await
this
.
room
.
startFrameSync
();
// return new Promise((resolve, reject) => {
// this.room.onStartFrameSync(() => {
// cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
// resolve(null);
// });
// });
}
async
stopFrameSync
()
{
await
this
.
room
.
stopFrameSync
();
console
.
log
(
"
停止帧同步成功
"
);
}
sendFrame
(
data
:
any
)
{
console
.
warn
(
'
sendFrame:
'
+
JSON
.
stringify
(
data
));
if
(
this
.
room
.
_isSyncing
)
{
this
.
room
.
sendFrame
(
JSON
.
stringify
(
data
));
}
else
{
console
.
log
(
'
is syncing = false
'
);
this
.
onRecvFrame
({
data
});
}
}
onJoinRoom
(
event
)
{
if
(
this
.
_eventListeners
[
"
playerJoin
"
])
{
this
.
_eventListeners
[
"
playerJoin
"
](
event
);
}
console
.
log
(
"
新玩家加入
"
,
JSON
.
stringify
(
event
));
this
.
room
.
players
.
forEach
(
player
=>
{
console
.
log
(
'
player.playerId =
'
,
player
.
playerId
);
});
}
onLeaveRoom
(
event
)
{
console
.
log
(
"
onLeaveRoom
"
);
if
(
this
.
_eventListeners
[
"
playerLeave
"
])
{
this
.
_eventListeners
[
"
playerLeave
"
](
event
);
}
console
.
log
(
"
玩家退出
"
,
event
.
data
.
leavePlayerId
);
}
onRecvFromClient
()
{
}
onDisconnect
(
event
)
{
this
.
log
(
"
玩家掉线了:
"
+
JSON
.
stringify
(
event
));
if
(
this
.
_eventListeners
[
"
playerOffLine
"
])
{
this
.
_eventListeners
[
"
playerOffLine
"
](
event
);
}
}
onRecvFrame
(
event
)
{
if
(
this
.
_eventListeners
[
"
frameEvent
"
])
{
this
.
_eventListeners
[
"
frameEvent
"
](
event
);
}
}
onStartFrameSync
(
event
)
{
console
.
log
(
'
onStartFrameSync
'
);
if
(
this
.
_eventListeners
[
"
gameStart
"
])
{
this
.
_eventListeners
[
"
gameStart
"
](
event
);
}
}
onStopFrameSync
(
event
)
{
}
onRecvFromGameSvr
()
{
}
async
onDestroy
()
{
try
{
console
.
log
(
"
onDestroy1
"
);
// MGOBE.Listener.clear();
this
.
stopFrameSync
();
this
.
closeRoom
();
this
.
dismissRoom
();
this
.
leaveRoom
();
console
.
log
(
"
onDestroy2
"
);
}
catch
(
e
)
{
console
.
log
(
JSON
.
stringify
(
e
));
}
}
log
(
str
)
{
const
node
=
cc
.
find
(
"
middleLayer
"
);
if
(
node
)
{
node
.
getComponent
(
"
middleLayer
"
).
log
(
str
);
}
else
{
cc
.
log
(
str
);
}
}
callNetworkApiGet
(
baseUrl
,
uri
,
data
,
callBack
)
{
let
queryStr
=
"
?
"
;
const
params
=
[];
for
(
const
key
in
data
)
{
if
(
Object
.
hasOwnProperty
.
call
(
data
,
key
))
{
params
.
push
(
`
${
key
}
=
${
data
[
key
]}
`
);
}
}
queryStr
+=
params
.
join
(
"
&
"
);
const
xhr
=
new
XMLHttpRequest
();
xhr
.
onreadystatechange
=
()
=>
{
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
>=
200
&&
xhr
.
status
<
400
)
{
callBack
(
xhr
.
responseText
);
}
};
const
url
=
`
${
baseUrl
}${
uri
}${
queryStr
}
`
;
console
.
log
(
"
url =
"
+
url
);
xhr
.
open
(
"
GET
"
,
url
,
true
);
xhr
.
send
();
}
}
export
function
asyncDelay
(
time
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
cc
.
tween
(
cc
.
find
(
"
Canvas
"
))
.
delay
(
time
)
.
call
(()
=>
{
resolve
(
null
);
})
.
start
();
});
}
assets/op_16/script/NetworkHelper2.ts.meta
0 → 100644
View file @
a690d2ac
{
"ver": "1.0.8",
"uuid": "e3b1625a-4a5d-493a-a856-9cce9f53169a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
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