@@ -1887,8 +1887,6 @@ var gs;
1887
1887
( function ( gs ) {
1888
1888
var Authentication = /** @class */ ( function ( ) {
1889
1889
function Authentication ( connection ) {
1890
- this . token = null ;
1891
- this . verificationCode = null ;
1892
1890
this . connection = connection ;
1893
1891
}
1894
1892
/**
@@ -1911,10 +1909,8 @@ var gs;
1911
1909
* @param message - 身份验证消息对象。
1912
1910
*/
1913
1911
Authentication . prototype . handleAuthenticationMessage = function ( message ) {
1914
- switch ( message . type ) {
1915
- case 'authentication' :
1916
- this . afterAuthenticated ( ) ;
1917
- break ;
1912
+ if ( message . payload . code == gs . ErrorCodes . SUCCESS ) {
1913
+ this . afterAuthenticated ( ) ;
1918
1914
}
1919
1915
} ;
1920
1916
/**
@@ -1952,6 +1948,16 @@ var gs;
1952
1948
gs . Connection = Connection ;
1953
1949
} ) ( gs || ( gs = { } ) ) ;
1954
1950
var gs ;
1951
+ ( function ( gs ) {
1952
+ gs . ErrorCodes = {
1953
+ SUCCESS : 'SUCCESS' ,
1954
+ AUTH_FAIL : 'AUTH_FAIL' ,
1955
+ WRONG_PASSWORD : 'WRONG_PASSWORD' ,
1956
+ REGISTRATION_FAILED : 'REGISTRATION_FAILED' ,
1957
+ RECONNECT_FAIL : 'RECONNECT_FAIL' ,
1958
+ } ;
1959
+ } ) ( gs || ( gs = { } ) ) ;
1960
+ var gs ;
1955
1961
( function ( gs ) {
1956
1962
var GNetworkAdapter = /** @class */ ( function ( ) {
1957
1963
function GNetworkAdapter ( serverUrl , username , password ) {
@@ -1961,9 +1967,18 @@ var gs;
1961
1967
this . sessionId = null ;
1962
1968
this . lastKnownState = null ;
1963
1969
this . connection = new gs . Connection ( serverUrl ) ;
1970
+ this . messageHandler = new gs . MessageHandler ( ) ;
1964
1971
this . authentication = new gs . Authentication ( this . connection ) ;
1972
+ this . roomAPI = new gs . RoomApi ( this ) ;
1965
1973
this . connect ( username , password ) ;
1966
1974
}
1975
+ Object . defineProperty ( GNetworkAdapter . prototype , "RoomAPI" , {
1976
+ get : function ( ) {
1977
+ return this . roomAPI ;
1978
+ } ,
1979
+ enumerable : true ,
1980
+ configurable : true
1981
+ } ) ;
1967
1982
GNetworkAdapter . prototype . connect = function ( username , password ) {
1968
1983
var _this = this ;
1969
1984
this . socket = this . connection . Socket ;
@@ -1995,8 +2010,11 @@ var gs;
1995
2010
} ) ;
1996
2011
this . socket . addEventListener ( 'message' , function ( event ) {
1997
2012
var message = JSON . parse ( event . data ) ;
2013
+ _this . messageHandler . emit ( message ) ;
1998
2014
if ( message . type === 'authentication' ) {
1999
- _this . sessionId = message . payload . sessionId ; // 存储sessionId
2015
+ if ( message . payload . code == gs . ErrorCodes . SUCCESS ) {
2016
+ _this . sessionId = message . payload . sessionId ; // 存储sessionId
2017
+ }
2000
2018
_this . authentication . handleAuthenticationMessage ( message ) ;
2001
2019
}
2002
2020
else if ( message . type === 'sessionId' ) {
@@ -2008,6 +2026,10 @@ var gs;
2008
2026
else if ( message . type == 'heartbeat' ) {
2009
2027
// 心跳包
2010
2028
}
2029
+ else if ( message . type == 'roomCreated' ) {
2030
+ // 房间创建
2031
+ _this . roomAPI . onRoomCreated ( message . payload . roomId ) ;
2032
+ }
2011
2033
else {
2012
2034
console . warn ( "[g-client]: \u672A\u77E5\u7684\u6D88\u606F\u7C7B\u578B: " + message . type ) ;
2013
2035
}
@@ -2082,6 +2104,66 @@ var gs;
2082
2104
gs . WebSocketUtils = WebSocketUtils ;
2083
2105
} ) ( gs || ( gs = { } ) ) ;
2084
2106
var gs ;
2107
+ ( function ( gs ) {
2108
+ var MessageHandler = /** @class */ ( function ( ) {
2109
+ function MessageHandler ( ) {
2110
+ }
2111
+ MessageHandler . prototype . emit = function ( message ) {
2112
+ this . handleMessage ( message ) ;
2113
+ } ;
2114
+ MessageHandler . prototype . on = function ( type , handler ) {
2115
+ this . messageHandlers [ type ] = handler ;
2116
+ } ;
2117
+ MessageHandler . prototype . handleMessage = function ( message ) {
2118
+ if ( ! this . messageHandlers )
2119
+ return ;
2120
+ var handler = this . messageHandlers [ message . type ] ;
2121
+ if ( handler ) {
2122
+ handler ( message ) ;
2123
+ }
2124
+ } ;
2125
+ return MessageHandler ;
2126
+ } ( ) ) ;
2127
+ gs . MessageHandler = MessageHandler ;
2128
+ } ) ( gs || ( gs = { } ) ) ;
2129
+ var gs ;
2130
+ ( function ( gs ) {
2131
+ var RoomApi = /** @class */ ( function ( ) {
2132
+ function RoomApi ( adapter ) {
2133
+ this . adapter = adapter ;
2134
+ this . createRoomCallback = null ;
2135
+ }
2136
+ RoomApi . prototype . createRoom = function ( maxPlayers , callback ) {
2137
+ this . createRoomCallback = callback ;
2138
+ var message = {
2139
+ type : 'createRoom' ,
2140
+ payload : { 'maxPlayers' : maxPlayers }
2141
+ } ;
2142
+ this . adapter . send ( message ) ;
2143
+ } ;
2144
+ RoomApi . prototype . joinRoom = function ( roomId , playerId ) {
2145
+ var message = {
2146
+ type : 'joinRoom' ,
2147
+ payload : { 'roomId' : roomId , 'playerId' : playerId }
2148
+ } ;
2149
+ this . adapter . send ( message ) ;
2150
+ } ;
2151
+ RoomApi . prototype . leaveRoom = function ( ) {
2152
+ } ;
2153
+ /**
2154
+ * 当房间创建成功时被调用
2155
+ * @param roomId - 房间ID
2156
+ */
2157
+ RoomApi . prototype . onRoomCreated = function ( roomId ) {
2158
+ if ( this . createRoomCallback ) {
2159
+ this . createRoomCallback ( roomId ) ;
2160
+ }
2161
+ } ;
2162
+ return RoomApi ;
2163
+ } ( ) ) ;
2164
+ gs . RoomApi = RoomApi ;
2165
+ } ) ( gs || ( gs = { } ) ) ;
2166
+ var gs ;
2085
2167
( function ( gs ) {
2086
2168
/**
2087
2169
* 快照插值策略
0 commit comments