@xmobitea/gn-typescript-client 2.6.12 → 2.6.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gearn.js.client.js +183 -57
- package/dist/gearn.js.client.min.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +183 -57
- package/dist/runtime/GNNetwork.d.ts +1 -1
- package/dist/runtime/helper/GNSupport.d.ts +20 -0
- package/dist/runtime/{entity → helper}/OperationHelper.d.ts +1 -1
- package/dist/runtime/helper/StorageService.d.ts +18 -0
- package/dist/runtime/networking/NetworkingPeer.d.ts +1 -1
- package/dist/runtime/typescript/ServiceUpdate.d.ts +2 -0
- package/docs/COCOS_CREATOR_INTEGRATION.md +116 -0
- package/examples/cocos-creator/GearNExample.ts.txt +176 -0
- package/package.json +3 -2
- package/srcSwift/Package.swift +32 -0
- package/srcSwift/Sources/GearN/runtime/GNNetwork.swift +530 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkAuthenticateApi.swift +178 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkCharacterPlayerApi.swift +1162 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkCloudScriptApi.swift +154 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkContentApi.swift +208 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkDashboardApi.swift +240 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkGamePlayerApi.swift +1369 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkGroupApi.swift +1100 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkInventoryApi.swift +937 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkMasterPlayerApi.swift +2323 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkMultiplayerApi.swift +298 -0
- package/srcSwift/Sources/GearN/runtime/GNNetworkStoreInventoryApi.swift +397 -0
- package/srcSwift/Sources/GearN/runtime/common/Action0.swift +3 -0
- package/srcSwift/Sources/GearN/runtime/common/Action1.swift +3 -0
- package/srcSwift/Sources/GearN/runtime/common/Action2.swift +3 -0
- package/srcSwift/Sources/GearN/runtime/common/Action3.swift +3 -0
- package/srcSwift/Sources/GearN/runtime/common/Action4.swift +3 -0
- package/srcSwift/Sources/GearN/runtime/common/GNArray.swift +204 -0
- package/srcSwift/Sources/GearN/runtime/common/GNData.swift +108 -0
- package/srcSwift/Sources/GearN/runtime/common/GNHashtable.swift +200 -0
- package/srcSwift/Sources/GearN/runtime/config/GNServerSettings.swift +95 -0
- package/srcSwift/Sources/GearN/runtime/constant/Commands.swift +28 -0
- package/srcSwift/Sources/GearN/runtime/constant/EventCode.swift +10 -0
- package/srcSwift/Sources/GearN/runtime/constant/OperationCode.swift +252 -0
- package/srcSwift/Sources/GearN/runtime/constant/ReturnCode.swift +19 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/ExecuteResponseStatus.swift +9 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/FriendStatus.swift +8 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/GoogleLoginType.swift +6 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/GroupStatus.swift +8 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/InvalidMemberType.swift +19 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/ItemType.swift +6 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/MatchmakingMemberStatus.swift +7 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/MatchmakingTicketStatus.swift +9 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/OwnerType.swift +10 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/PermissionDataItem.swift +6 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/PushPlatformType.swift +6 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/RequestRole.swift +7 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/RequestType.swift +16 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/StoreItemType.swift +6 -0
- package/srcSwift/Sources/GearN/runtime/constant/enumType/StoreReceiveType.swift +9 -0
- package/srcSwift/Sources/GearN/runtime/constant/errorCode/ErrorCode.swift +58 -0
- package/srcSwift/Sources/GearN/runtime/constant/parameterCode/ParameterCode.swift +672 -0
- package/srcSwift/Sources/GearN/runtime/entity/DataMember.swift +196 -0
- package/srcSwift/Sources/GearN/runtime/entity/GNMetadata.swift +9 -0
- package/srcSwift/Sources/GearN/runtime/entity/InvalidMember.swift +11 -0
- package/srcSwift/Sources/GearN/runtime/entity/OperationEvent.swift +38 -0
- package/srcSwift/Sources/GearN/runtime/entity/OperationHelper.swift +28 -0
- package/srcSwift/Sources/GearN/runtime/entity/OperationRequest.swift +62 -0
- package/srcSwift/Sources/GearN/runtime/entity/OperationResponse.swift +98 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/AuthenticateModels.swift +351 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/AuthenticateRequestModels.swift +81 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/AuthenticateResponseModels.swift +108 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CharacterPlayerModels.swift +1045 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CharacterPlayerRequestModels.swift +821 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CharacterPlayerResponseModels.swift +588 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CloudScriptModels.swift +187 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CloudScriptRequestModels.swift +84 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/CloudScriptResponseModels.swift +59 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/ContentModels.swift +195 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/ContentRequestModels.swift +116 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/ContentResponseModels.swift +81 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/DashboardModels.swift +426 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/DashboardRequestModels.swift +160 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/DashboardResponseModels.swift +82 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GamePlayerModels.swift +1334 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GamePlayerRequestModels.swift +643 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GamePlayerResponseModels.swift +213 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GenericModels.swift +171 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GroupModels.swift +850 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GroupRequestModels.swift +485 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/GroupResponseModels.swift +165 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/InventoryModels.swift +679 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/InventoryRequestModels.swift +413 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/InventoryResponseModels.swift +141 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MasterPlayerModels.swift +378 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MasterPlayerRequestModels.swift +147 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MasterPlayerResponseModels.swift +318 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MultiplayerModels.swift +319 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MultiplayerRequestModels.swift +125 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/MultiplayerResponseModels.swift +45 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/StoreInventoryModels.swift +633 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/StoreInventoryRequestModels.swift +173 -0
- package/srcSwift/Sources/GearN/runtime/entity/models/StoreInventoryResponseModels.swift +61 -0
- package/srcSwift/Sources/GearN/runtime/entity/request/CustomOperationRequest.swift +42 -0
- package/srcSwift/Sources/GearN/runtime/entity/response/CustomOperationResponse.swift +49 -0
- package/srcSwift/Sources/GearN/runtime/entity/response/GetAuthInfoResponse.swift +43 -0
- package/srcSwift/Sources/GearN/runtime/entity/response/HealthCheckResponse.swift +86 -0
- package/srcSwift/Sources/GearN/runtime/entity/response/UploadFileResponse.swift +15 -0
- package/srcSwift/Sources/GearN/runtime/helper/CodeHelper.swift +107 -0
- package/srcSwift/Sources/GearN/runtime/helper/ConverterService.swift +98 -0
- package/srcSwift/Sources/GearN/runtime/helper/EnumUtility.swift +34 -0
- package/srcSwift/Sources/GearN/runtime/helper/GNSupport.swift +41 -0
- package/srcSwift/Sources/GearN/runtime/helper/GNUtils.swift +66 -0
- package/srcSwift/Sources/GearN/runtime/helper/MessagePackConverterService.swift +21 -0
- package/srcSwift/Sources/GearN/runtime/helper/StorageService.swift +29 -0
- package/srcSwift/Sources/GearN/runtime/logger/GNDebug.swift +33 -0
- package/srcSwift/Sources/GearN/runtime/networking/AuthenticateStatus.swift +24 -0
- package/srcSwift/Sources/GearN/runtime/networking/IPeer.swift +8 -0
- package/srcSwift/Sources/GearN/runtime/networking/NetworkingPeer.swift +368 -0
- package/srcSwift/Sources/GearN/runtime/networking/OperationPending.swift +81 -0
- package/srcSwift/Sources/GearN/runtime/networking/PeerBase.swift +228 -0
- package/srcSwift/Sources/GearN/runtime/networking/handler/IServerEventHandler.swift +20 -0
- package/srcSwift/Sources/GearN/runtime/networking/http/HttpPeer.swift +226 -0
- package/srcSwift/Sources/GearN/runtime/networking/http/HttpTypes.swift +24 -0
- package/srcSwift/Sources/GearN/runtime/networking/http/NetworkingHttpPeerBase.swift +13 -0
- package/srcSwift/Sources/GearN/runtime/networking/http/NetworkingPeerUrlSession.swift +125 -0
- package/srcSwift/Sources/GearN/runtime/networking/request/NetRequest.swift +19 -0
- package/srcSwift/Sources/GearN/runtime/networking/response/NetResponse.swift +13 -0
- package/srcSwift/Sources/GearN/runtime/networking/socket/NetworkingPeerSocketIOClient.swift +244 -0
- package/srcSwift/Sources/GearN/runtime/networking/socket/NetworkingSocketPeerBase.swift +59 -0
- package/srcSwift/Sources/GearN/runtime/networking/socket/SocketPeer.swift +136 -0
- package/tsconfig-build.cocos.json +31 -0
- package/webpack.config.cocos.mjs +78 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
// import FoundationNetworking // Linux support if needed, but Foundation usually suffices on Darwin
|
|
3
|
+
|
|
4
|
+
public class NetworkingPeerUrlSession: NetworkingHttpPeerBase {
|
|
5
|
+
private var httpUrl: String = ""
|
|
6
|
+
private var userAgent: String = ""
|
|
7
|
+
private var useReverseProxy: Bool = false
|
|
8
|
+
|
|
9
|
+
private let session = URLSession.shared
|
|
10
|
+
// Or create custom configuration if needed
|
|
11
|
+
|
|
12
|
+
public init() {}
|
|
13
|
+
|
|
14
|
+
public func initPeer(httpUrl: String, userAgent: String) {
|
|
15
|
+
self.httpUrl = httpUrl
|
|
16
|
+
self.userAgent = userAgent
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public func setUseReverseProxy(useReverseProxy: Bool) {
|
|
20
|
+
self.useReverseProxy = useReverseProxy
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public func getRequest(subUri: String, onHttpAppResponse: @escaping Action1<HttpAppResponse>, timeout: Int, authToken: String, secretKey: String, gameId: String) {
|
|
24
|
+
let urlD = self.useReverseProxy ? "\(self.httpUrl)/\(subUri)" : "\(self.httpUrl)/\(subUri)"
|
|
25
|
+
// Logic for reverse proxy might be different in TS, assuming append for now
|
|
26
|
+
|
|
27
|
+
guard let url = URL(string: urlD) else {
|
|
28
|
+
let res = HttpAppResponse()
|
|
29
|
+
res.error = "Invalid URL"
|
|
30
|
+
onHttpAppResponse(res)
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var request = URLRequest(url: url)
|
|
35
|
+
request.httpMethod = "GET"
|
|
36
|
+
request.timeoutInterval = TimeInterval(timeout)
|
|
37
|
+
|
|
38
|
+
request.setValue(self.userAgent, forHTTPHeaderField: Commands.USER_AGENT)
|
|
39
|
+
if !authToken.isEmpty { request.setValue(authToken, forHTTPHeaderField: Commands.RequestAuthTokenCmd) }
|
|
40
|
+
if !secretKey.isEmpty { request.setValue(secretKey, forHTTPHeaderField: Commands.RequestSecretCmd) }
|
|
41
|
+
if !gameId.isEmpty { request.setValue(gameId, forHTTPHeaderField: Commands.RequestGameIdCmd) }
|
|
42
|
+
|
|
43
|
+
let task = self.session.dataTask(with: request) { data, response, error in
|
|
44
|
+
let res = HttpAppResponse()
|
|
45
|
+
if let error = error {
|
|
46
|
+
res.error = error.localizedDescription
|
|
47
|
+
} else if let httpResponse = response as? HTTPURLResponse {
|
|
48
|
+
res.statusCode = httpResponse.statusCode
|
|
49
|
+
res.data = data
|
|
50
|
+
if let data = data {
|
|
51
|
+
res.text = String(data: data, encoding: .utf8)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
onHttpAppResponse(res)
|
|
55
|
+
}
|
|
56
|
+
task.resume()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public func postRequest(subUri: String, param: GNHashtable, postType: PostType, onHttpAppResponse: @escaping Action1<HttpAppResponse>, timeout: Int, authToken: String, secretKey: String, customTags: GNHashtable?, gameId: String) {
|
|
60
|
+
let urlD = "\(self.httpUrl)/\(subUri)"
|
|
61
|
+
guard let url = URL(string: urlD) else {
|
|
62
|
+
let res = HttpAppResponse()
|
|
63
|
+
res.error = "Invalid URL"
|
|
64
|
+
onHttpAppResponse(res)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var request = URLRequest(url: url)
|
|
69
|
+
request.httpMethod = "POST"
|
|
70
|
+
request.timeoutInterval = TimeInterval(timeout)
|
|
71
|
+
|
|
72
|
+
request.setValue(self.userAgent, forHTTPHeaderField: Commands.USER_AGENT)
|
|
73
|
+
if !authToken.isEmpty { request.setValue(authToken, forHTTPHeaderField: Commands.RequestAuthTokenCmd) }
|
|
74
|
+
if !secretKey.isEmpty { request.setValue(secretKey, forHTTPHeaderField: Commands.RequestSecretCmd) }
|
|
75
|
+
if !gameId.isEmpty { request.setValue(gameId, forHTTPHeaderField: Commands.RequestGameIdCmd) }
|
|
76
|
+
|
|
77
|
+
if let tags = customTags {
|
|
78
|
+
if let jsonTags = try? JSONSerialization.data(withJSONObject: tags.toData(), options: []) {
|
|
79
|
+
if let strTags = String(data: jsonTags, encoding: .utf8) {
|
|
80
|
+
request.setValue(strTags, forHTTPHeaderField: Commands.CustomTags)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if postType == .Json {
|
|
86
|
+
request.setValue(Commands.APPLICATION_JSON, forHTTPHeaderField: Commands.CONTENT_TYPE)
|
|
87
|
+
if let body = try? JSONSerialization.data(withJSONObject: param.toData(), options: []) {
|
|
88
|
+
request.httpBody = body
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
// MsgPack
|
|
92
|
+
request.setValue(Commands.APPLICATION_MSGPACK, forHTTPHeaderField: Commands.CONTENT_TYPE)
|
|
93
|
+
// Need MessagePack serializer.
|
|
94
|
+
// TS uses MessagePackConverterService.serialize(param.toData())
|
|
95
|
+
// For now assume JSON or stub
|
|
96
|
+
let res = HttpAppResponse()
|
|
97
|
+
res.error = "MsgPack not implemented yet"
|
|
98
|
+
onHttpAppResponse(res)
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let task = self.session.dataTask(with: request) { data, response, error in
|
|
103
|
+
let res = HttpAppResponse()
|
|
104
|
+
if let error = error {
|
|
105
|
+
res.error = error.localizedDescription
|
|
106
|
+
} else if let httpResponse = response as? HTTPURLResponse {
|
|
107
|
+
res.statusCode = httpResponse.statusCode
|
|
108
|
+
res.data = data
|
|
109
|
+
if let data = data {
|
|
110
|
+
res.text = String(data: data, encoding: .utf8)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
onHttpAppResponse(res)
|
|
114
|
+
}
|
|
115
|
+
task.resume()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public func postRequestUpload(subUri: String, content: Data, filename: String, mimetype: String, onHttpAppResponse: @escaping Action1<HttpAppResponse>, timeout: Int) {
|
|
119
|
+
// Implementation for upload (multipart)
|
|
120
|
+
// Stub for now or simple manual multipart construction
|
|
121
|
+
let res = HttpAppResponse()
|
|
122
|
+
res.error = "Upload not implemented"
|
|
123
|
+
onHttpAppResponse(res)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public class NetRequest {
|
|
4
|
+
private var operationRequest: OperationRequest
|
|
5
|
+
private var completion: ((OperationResponse) -> Void)?
|
|
6
|
+
|
|
7
|
+
public init(operationRequest: OperationRequest, completion: ((OperationResponse) -> Void)? = nil) {
|
|
8
|
+
self.operationRequest = operationRequest
|
|
9
|
+
self.completion = completion
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public func getOperationRequest() -> OperationRequest {
|
|
13
|
+
return self.operationRequest
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public func getCompletion() -> ((OperationResponse) -> Void)? {
|
|
17
|
+
return self.completion
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public class NetResponse {
|
|
4
|
+
private var operationResponse: OperationResponse
|
|
5
|
+
|
|
6
|
+
public init(operationResponse: OperationResponse) {
|
|
7
|
+
self.operationResponse = operationResponse
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public func getOperationResponse() -> OperationResponse {
|
|
11
|
+
return self.operationResponse
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import SocketIO
|
|
3
|
+
|
|
4
|
+
public class NetworkingPeerSocketIOClient: NetworkingSocketPeerBase {
|
|
5
|
+
public var clientId: String = ""
|
|
6
|
+
public var isWsConnected: Bool {
|
|
7
|
+
return self.socketManager?.status == .connected
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
private var url: String = ""
|
|
11
|
+
private var reconnectDelay: Int = 1000
|
|
12
|
+
private var pingInterval: Int = 25000
|
|
13
|
+
private var pingTimeout: Int = 60000
|
|
14
|
+
|
|
15
|
+
private var onConnect: Action0?
|
|
16
|
+
private var onDisconnect: Action0?
|
|
17
|
+
|
|
18
|
+
private var onEventHandler: Action1<GNHashtable>?
|
|
19
|
+
private var onResponseHandler: Action1<GNHashtable>?
|
|
20
|
+
|
|
21
|
+
private var manager: SocketManager?
|
|
22
|
+
private var socketManager: SocketIOClient?
|
|
23
|
+
|
|
24
|
+
// Lock for thread safety if needed, though SocketIOClient is mostly main queue or handled internally.
|
|
25
|
+
// For simplicity in this port, we rely on SocketIO callbacks.
|
|
26
|
+
|
|
27
|
+
public init() {}
|
|
28
|
+
|
|
29
|
+
public func initPeer(url: String, reconnectDelay: Int, pingInterval: Int, pingTimeout: Int) {
|
|
30
|
+
self.url = url
|
|
31
|
+
self.reconnectDelay = reconnectDelay
|
|
32
|
+
self.pingInterval = pingInterval
|
|
33
|
+
self.pingTimeout = pingTimeout
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public func connect(_onSocketConnect: @escaping Action0) {
|
|
37
|
+
self.onConnect = _onSocketConnect
|
|
38
|
+
|
|
39
|
+
if self.isWsConnected {
|
|
40
|
+
GNDebug.log(message: "[GN Socket] Reconnecting")
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
GNDebug.log(message: "[GN Socket] Connecting via SocketIO client")
|
|
45
|
+
|
|
46
|
+
if self.manager == nil {
|
|
47
|
+
guard let urlObj = URL(string: self.url) else {
|
|
48
|
+
GNDebug.log(message: "[GN Socket] Invalid URL: \(self.url)")
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// SocketIO Config
|
|
53
|
+
// Unity SDK uses: Path = "/socket.io", EIO = 4, Transport = Websocket
|
|
54
|
+
var config: SocketIOClientConfiguration = [
|
|
55
|
+
.path("/socket.io"),
|
|
56
|
+
.connectParams([:]), // Add auth/secret here if needed on handshake? No, standard GearN uses RequestAuthTokenCmd
|
|
57
|
+
.reconnects(true),
|
|
58
|
+
.reconnectWait(Int(self.reconnectDelay / 1000)), // library uses seconds? check docs. actually Swift SocketIO uses Int seconds usually or check.
|
|
59
|
+
// .reconnectWait accepts Int (seconds) in some versions, check compatibility.
|
|
60
|
+
// Assuming standard defaults or simple config.
|
|
61
|
+
.forceWebsockets(true),
|
|
62
|
+
.log(false)
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
// Reconnection delay in SocketIO-Swift is usually 'reconnectWait' (seconds).
|
|
66
|
+
|
|
67
|
+
self.manager = SocketManager(socketURL: urlObj, config: config)
|
|
68
|
+
self.socketManager = self.manager?.defaultSocket
|
|
69
|
+
|
|
70
|
+
setupListeners()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
self.socketManager?.connect()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private func setupListeners() {
|
|
77
|
+
guard let socket = self.socketManager else { return }
|
|
78
|
+
|
|
79
|
+
socket.on(clientEvent: .connect) { [weak self] data, ack in
|
|
80
|
+
guard let self = self else { return }
|
|
81
|
+
GNDebug.log(message: "[GN Socket RECV] HandleOpen \(socket.sid ?? "")")
|
|
82
|
+
self.clientId = socket.sid ?? ""
|
|
83
|
+
self.onConnect?()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
socket.on(clientEvent: .disconnect) { [weak self] data, ack in
|
|
87
|
+
guard let self = self else { return }
|
|
88
|
+
GNDebug.log(message: "[GN Socket RECV] HandleClose")
|
|
89
|
+
self.clientId = ""
|
|
90
|
+
self.onDisconnect?()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
socket.on(clientEvent: .error) { data, ack in
|
|
94
|
+
GNDebug.log(message: "[GN Socket RECV] Handle Error \(data)")
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Listen for standard GearN events (JSON)
|
|
98
|
+
// Commands.ResponseCmd_Json = "rJson"
|
|
99
|
+
// Commands.EventCmd_Json = "eJson"
|
|
100
|
+
|
|
101
|
+
socket.on("rJson") { [weak self] data, ack in
|
|
102
|
+
self?.handleResponseJson(data)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
socket.on("eJson") { [weak self] data, ack in
|
|
106
|
+
self?.handleEventJson(data)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// MsgPack not implemented in this pass without a MsgPack library
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private func handleResponseJson(_ data: [Any]) {
|
|
113
|
+
// Data is usually [responseObj] or [code, params, ...] depending on server emit.
|
|
114
|
+
// Unity SDK: originData = response.GetValue<JsonElement>(0);
|
|
115
|
+
// Swift SocketIO passes the args array.
|
|
116
|
+
// We expect data[0] to be the payload.
|
|
117
|
+
|
|
118
|
+
guard let payload = data.first else { return }
|
|
119
|
+
|
|
120
|
+
// Convert payload (Any) to GNHashtable?
|
|
121
|
+
// Wait, onResponseHandler expects Action1<GNHashtable>?
|
|
122
|
+
// Actually, NetworkingSocketPeerBase defines: func setOnResponseHandler(_onResponseHandler: @escaping Action1<GNHashtable>)
|
|
123
|
+
// But standard SocketPacket handling in GearN usually separates header/body.
|
|
124
|
+
// User's code 'NetworkingSocketPeerBase' defines handlers taking GNHashtable.
|
|
125
|
+
// But typically response is a structured object.
|
|
126
|
+
// Let's assume we convert the Dict/Array to GNHashtable to pass up.
|
|
127
|
+
|
|
128
|
+
if let dict = payload as? [String: Any] {
|
|
129
|
+
let gnTable = GNData.createGNHashtableFromObject(value: dict)
|
|
130
|
+
self.onResponseHandler?(gnTable)
|
|
131
|
+
} else if let arr = payload as? [Any] {
|
|
132
|
+
// Should not happen for a structured response unless it's a list.
|
|
133
|
+
// Maybe wrap in a hashtable with "data"?
|
|
134
|
+
let gnTable = GNHashtable()
|
|
135
|
+
gnTable.put("data", arr) // Ad-hoc
|
|
136
|
+
self.onResponseHandler?(gnTable)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private func handleEventJson(_ data: [Any]) {
|
|
141
|
+
guard let payload = data.first else { return }
|
|
142
|
+
|
|
143
|
+
if let dict = payload as? [String: Any] {
|
|
144
|
+
let gnTable = GNData.createGNHashtableFromObject(value: dict)
|
|
145
|
+
self.onEventHandler?(gnTable)
|
|
146
|
+
} else if let arr = payload as? [Any] {
|
|
147
|
+
// Events might be [code, data]
|
|
148
|
+
// Unity SDK: originData.GetArrayLength() > 1 ? new object[] { code, data } ...
|
|
149
|
+
// We need to pass this up properly.
|
|
150
|
+
// If GNHashtable is the only transport, we might map array indices to keys "0", "1".
|
|
151
|
+
// Or better, Protocol should define what onEventHandler expects.
|
|
152
|
+
// Looking at `NetworkingPeer.swift` (previous knowledge), it often processes the packet.
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public func close(_onSocketDisconnect: @escaping Action0) {
|
|
157
|
+
self.onDisconnect = _onSocketDisconnect
|
|
158
|
+
|
|
159
|
+
GNDebug.log(message: "[GN Socket] Disconnecting via SocketIO client")
|
|
160
|
+
|
|
161
|
+
if let socket = self.socketManager, socket.status == .connected {
|
|
162
|
+
socket.disconnect()
|
|
163
|
+
} else {
|
|
164
|
+
self.onDisconnect?()
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public func sendRequestAuthSocket() {
|
|
169
|
+
// Commands.RequestAuthTokenCmd = "Auth-Token"
|
|
170
|
+
guard let authToken = GNNetwork.getAuthenticateStatus().getAuthToken() else { return }
|
|
171
|
+
self.socketManager?.emit("Auth-Token", authToken)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public func send(requestType: RequestType, role: RequestRole, operationRequest: OperationRequest, authToken: String, secretKey: String, customTags: GNHashtable?, gameId: String) {
|
|
175
|
+
// Construct payload
|
|
176
|
+
// OperationHelper.toSocketData equivalent
|
|
177
|
+
|
|
178
|
+
let typeName = CodeHelper.getRequestTypeName(requestType.rawValue)
|
|
179
|
+
let roleName = CodeHelper.getRequestRoleName(role.rawValue)
|
|
180
|
+
|
|
181
|
+
var payload: [Any] = [
|
|
182
|
+
typeName,
|
|
183
|
+
roleName,
|
|
184
|
+
operationRequest.getOperationCode()
|
|
185
|
+
]
|
|
186
|
+
|
|
187
|
+
if let params = operationRequest.getParameters() {
|
|
188
|
+
payload.append(params.toData())
|
|
189
|
+
} else {
|
|
190
|
+
payload.append(NSNull())
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if operationRequest.getRequestId() != -1 {
|
|
194
|
+
payload.append(operationRequest.getRequestId())
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Emit "rJson"
|
|
198
|
+
self.socketManager?.emit("rJson", payload)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public func reInitNewSocket() {
|
|
202
|
+
// Re-create manager if needed, or just update config.
|
|
203
|
+
// For now, we can rely on existing manager unless URL changed.
|
|
204
|
+
if self.manager == nil {
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
// If needed to force new connection params:
|
|
208
|
+
self.manager?.disconnect()
|
|
209
|
+
self.manager = nil
|
|
210
|
+
self.socketManager = nil
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public func setOnConnectHandler(_onConnectHandler: @escaping Action0) {
|
|
214
|
+
// In this architecture, it seems we pass callback in connect(), or set it here.
|
|
215
|
+
// Unity SDK: this.onConnectHandler = ...
|
|
216
|
+
// We'll support setting it here too.
|
|
217
|
+
// But connect() overrides it in my impl above.
|
|
218
|
+
// Let's make sure we support both or stick to interface.
|
|
219
|
+
// Interface has separate setters.
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public func setOnDisconnectHandler(_onDisconnectHandler: @escaping Action0) {
|
|
223
|
+
// ...
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
public func removeOnConnectHandler(_onConnectHandler: @escaping Action0) { }
|
|
227
|
+
public func removeOnDisconnectHandler(_onDisconnectHandler: @escaping Action0) { }
|
|
228
|
+
|
|
229
|
+
public func setOnEventHandler(_onEventHandler: @escaping Action1<GNHashtable>) {
|
|
230
|
+
self.onEventHandler = _onEventHandler
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public func setOnResponseHandler(_onResponseHandler: @escaping Action1<GNHashtable>) {
|
|
234
|
+
self.onResponseHandler = _onResponseHandler
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public func service() {
|
|
238
|
+
// SocketIOClientSwift handles its own runloop/thread usually.
|
|
239
|
+
// No explicit service poll needed unless we need to dispatch to main thread manually.
|
|
240
|
+
// We'll leave empty for now.
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public class SocketPacket {
|
|
4
|
+
public var obj: Any
|
|
5
|
+
public var isMsgPack: Bool
|
|
6
|
+
|
|
7
|
+
public init(obj: Any, isMsgPack: Bool) {
|
|
8
|
+
self.obj = obj
|
|
9
|
+
self.isMsgPack = isMsgPack
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public class Action0List {
|
|
14
|
+
private var handlers: [Action0] = []
|
|
15
|
+
|
|
16
|
+
public func subscriber(action: @escaping Action0) {
|
|
17
|
+
self.handlers.append(action)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public func unsubscriber(action: @escaping Action0) {
|
|
21
|
+
// Swift closures are difficult to compare for equality.
|
|
22
|
+
// This simple removal might not work as intended without using objects/wrappers.
|
|
23
|
+
// For SDK port, we'll skip exact removal implementation or use a token/uuid system.
|
|
24
|
+
// Or if Action0 is a wrapper class, we can compare.
|
|
25
|
+
// Assuming loose implementation for now.
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public func invoke() {
|
|
29
|
+
self.handlers.forEach { $0() }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public init() {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public protocol NetworkingSocketPeerBase {
|
|
36
|
+
var clientId: String { get }
|
|
37
|
+
var isWsConnected: Bool { get }
|
|
38
|
+
|
|
39
|
+
func initPeer(url: String, reconnectDelay: Int, pingInterval: Int, pingTimeout: Int)
|
|
40
|
+
|
|
41
|
+
func connect(_onSocketConnect: @escaping Action0)
|
|
42
|
+
func close(_onSocketDisconnect: @escaping Action0)
|
|
43
|
+
|
|
44
|
+
func sendRequestAuthSocket()
|
|
45
|
+
|
|
46
|
+
func send(requestType: RequestType, role: RequestRole, operationRequest: OperationRequest, authToken: String, secretKey: String, customTags: GNHashtable?, gameId: String)
|
|
47
|
+
|
|
48
|
+
func reInitNewSocket()
|
|
49
|
+
|
|
50
|
+
func setOnConnectHandler(_onConnectHandler: @escaping Action0)
|
|
51
|
+
func setOnDisconnectHandler(_onDisconnectHandler: @escaping Action0)
|
|
52
|
+
func removeOnConnectHandler(_onConnectHandler: @escaping Action0)
|
|
53
|
+
func removeOnDisconnectHandler(_onDisconnectHandler: @escaping Action0)
|
|
54
|
+
|
|
55
|
+
func setOnEventHandler(_onEventHandler: @escaping Action1<GNHashtable>)
|
|
56
|
+
func setOnResponseHandler(_onResponseHandler: @escaping Action1<GNHashtable>)
|
|
57
|
+
|
|
58
|
+
func service()
|
|
59
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public class SocketPeer: PeerBase {
|
|
4
|
+
private var networkingSocketPeerBase: NetworkingSocketPeerBase!
|
|
5
|
+
|
|
6
|
+
private var serverEventHandlersDict: [String: [IServerEventHandler]] = [:]
|
|
7
|
+
private var _onEventHandler: Action1<OperationEvent>?
|
|
8
|
+
|
|
9
|
+
public override func initGNSocketObject() {
|
|
10
|
+
guard let gnServerSettings = GNNetwork.getGNServerSettings() else {
|
|
11
|
+
fatalError("Where is GN Server Settings")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
self.isUse = gnServerSettings.isUseSocket()
|
|
15
|
+
|
|
16
|
+
self.networkingSocketPeerBase = NetworkingPeerSocketIOClient()
|
|
17
|
+
|
|
18
|
+
self.networkingSocketPeerBase.initPeer(url: gnServerSettings.getSocketUrl(), reconnectDelay: gnServerSettings.getReconnectDelay(), pingInterval: gnServerSettings.getPingInterval(), pingTimeout: gnServerSettings.getPingTimeout())
|
|
19
|
+
|
|
20
|
+
self.networkingSocketPeerBase.reInitNewSocket()
|
|
21
|
+
|
|
22
|
+
self.networkingSocketPeerBase.setOnEventHandler { [weak self] t1 in
|
|
23
|
+
self?.onEventHandler(obj: t1)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
self.networkingSocketPeerBase.setOnResponseHandler { [weak self] t1 in
|
|
27
|
+
self?.onResponseHandler(obj: t1)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
self.subscriberServerEventHandler()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private func subscriberServerEventHandler() {
|
|
34
|
+
self.serverEventHandlersDict = [:]
|
|
35
|
+
|
|
36
|
+
let serverEventHandlers = ServerEventHandlerRegistry.getImplementations()
|
|
37
|
+
for handlerType in serverEventHandlers {
|
|
38
|
+
let handler = handlerType.init() // Assuming Required Init or similar protocol requirement
|
|
39
|
+
// protocol IServerEventHandler needs init() requirement? or just generic init.
|
|
40
|
+
// In Swift: T() requires init() in protocol or class.
|
|
41
|
+
// IServerEventHandler is protocol. We need a way to instantiate.
|
|
42
|
+
// Changing IServerEventHandler to require init()
|
|
43
|
+
|
|
44
|
+
self.subscriberAServerEventHandler(serverEventHandler: handler)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public func subscriberAServerEventHandler(serverEventHandler: IServerEventHandler) {
|
|
49
|
+
var handlers = self.serverEventHandlersDict[serverEventHandler.getEventCode()] ?? []
|
|
50
|
+
handlers.append(serverEventHandler)
|
|
51
|
+
self.serverEventHandlersDict[serverEventHandler.getEventCode()] = handlers
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public override func send(operationPending: OperationPending) {
|
|
55
|
+
guard let gnServerSettings = GNNetwork.getGNServerSettings() else { return }
|
|
56
|
+
|
|
57
|
+
let operationRequest = operationPending.getOperationRequest()
|
|
58
|
+
if operationRequest.getTimeout() == OperationRequest.defaultTimeOut {
|
|
59
|
+
let _ = operationRequest.setTimeout(timeout: gnServerSettings.getDefaultTimeoutInSeconds())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
super.send(operationPending: operationPending)
|
|
63
|
+
|
|
64
|
+
GNDebug.log("[GN Socket SEND] " + operationRequest.toString())
|
|
65
|
+
|
|
66
|
+
let authToken = operationPending.getAuthToken()
|
|
67
|
+
let secretKey = operationPending.getSecretKey()
|
|
68
|
+
let gameId = operationPending.getGameId()
|
|
69
|
+
let customTags = operationPending.getCustomTags()
|
|
70
|
+
|
|
71
|
+
self.networkingSocketPeerBase.send(requestType: operationPending.getRequestType(), role: operationPending.getRole(), operationRequest: operationRequest, authToken: authToken, secretKey: secretKey, customTags: customTags, gameId: gameId)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public override func onEnqueue(operationPending: OperationPending) {
|
|
75
|
+
GNDebug.log("[GN Socket ENQUEUE] " + operationPending.getOperationRequest().toString())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public func onEventHandler(obj: GNHashtable) {
|
|
79
|
+
guard let eventCode = obj.getString(ParameterCode.EventCode) else { return }
|
|
80
|
+
|
|
81
|
+
let operationEvent = OperationEvent(eventCode: eventCode, parameters: obj.getGNHashtable(ParameterCode.Parameters))
|
|
82
|
+
|
|
83
|
+
GNDebug.log("[GN Socket EVENT] " + operationEvent.toString())
|
|
84
|
+
|
|
85
|
+
if let handler = self._onEventHandler {
|
|
86
|
+
handler(operationEvent)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if let handlers = self.serverEventHandlersDict[eventCode] {
|
|
90
|
+
handlers.forEach { $0.handle(operationEvent: operationEvent) }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public override func service() {
|
|
95
|
+
super.service()
|
|
96
|
+
self.networkingSocketPeerBase.service()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Helper accessors
|
|
100
|
+
public func getClientId() -> String {
|
|
101
|
+
return self.networkingSocketPeerBase?.clientId ?? ""
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public func getPing() -> Int {
|
|
105
|
+
return self.networkingSocketPeerBase?.ping ?? -1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public func isConnected() -> Bool {
|
|
109
|
+
return self.networkingSocketPeerBase?.isWsConnected ?? false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public func connect(onConnect: Action0? = nil) {
|
|
113
|
+
if let callback = onConnect {
|
|
114
|
+
self.networkingSocketPeerBase?.connect(_onSocketConnect: callback)
|
|
115
|
+
} else {
|
|
116
|
+
self.networkingSocketPeerBase?.connect(_onSocketConnect: {})
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public func disconnect(onDisconnect: Action0? = nil) {
|
|
121
|
+
if let callback = onDisconnect {
|
|
122
|
+
self.networkingSocketPeerBase?.close(_onSocketDisconnect: callback)
|
|
123
|
+
} else {
|
|
124
|
+
self.networkingSocketPeerBase?.close(_onSocketDisconnect: {})
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public func sendRequestAuth() {
|
|
129
|
+
self.networkingSocketPeerBase?.sendRequestAuth()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public func setOnEventHandler(handler: Action1<OperationEvent>?) {
|
|
133
|
+
self._onEventHandler = handler
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2017",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationDir": "./dist",
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"lib": [
|
|
10
|
+
"ES2017",
|
|
11
|
+
"DOM"
|
|
12
|
+
],
|
|
13
|
+
"moduleResolution": "node",
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"skipLibCheck": true
|
|
16
|
+
},
|
|
17
|
+
"include": [
|
|
18
|
+
"./src/**/*.ts"
|
|
19
|
+
],
|
|
20
|
+
"exclude": [
|
|
21
|
+
"./test/",
|
|
22
|
+
"./node_modules/",
|
|
23
|
+
"./dist/",
|
|
24
|
+
"./index.js",
|
|
25
|
+
"./webpack.config.udm.mjs",
|
|
26
|
+
"./webpack.config.min.udm.mjs",
|
|
27
|
+
"./webpack.config.mjs",
|
|
28
|
+
"./webpack.config.cocos.mjs",
|
|
29
|
+
"./syncCode.js",
|
|
30
|
+
]
|
|
31
|
+
}
|