55using HuaJiBot . NET . Adapter . Satori . Protocol . Events ;
66using HuaJiBot . NET . Commands ;
77using HuaJiBot . NET . Events ;
8+ using HuaJiBot . NET . Websocket ;
89using Newtonsoft . Json ;
910using Newtonsoft . Json . Linq ;
1011using Newtonsoft . Json . Serialization ;
11- using Websocket . Client ;
1212using Timer = System . Timers . Timer ;
1313
1414namespace HuaJiBot . NET . Adapter . Satori . Protocol ;
@@ -26,79 +26,63 @@ internal class SatoriEventClient
2626 private readonly Timer _pingTimer ;
2727 private readonly SatoriAdapter _service ;
2828
29- public Task ConnectAsync ( ) => _client . Start ( ) ;
29+ public Task ConnectAsync ( ) => Task . CompletedTask ;
3030
3131 public SatoriEventClient ( SatoriAdapter service , Uri wsUrl , string token )
3232 {
33- _client = new WebsocketClient ( wsUrl )
34- {
35- IsTextMessageConversionEnabled = true ,
36- MessageEncoding = Encoding . UTF8 ,
37- ReconnectTimeout = null ,
38- } ;
33+ _client = new ( wsUrl ) ;
3934 _service = service ;
40- _client
41- . MessageReceived . Where ( m => m . MessageType == WebSocketMessageType . Text )
42- . Select ( m => m . Text )
43- . Subscribe ( msg =>
35+
36+ // 订阅消息接收事件
37+ _client . OnMessage += async msg =>
38+ {
39+ try
4440 {
45- try
46- {
47- ProcessMessageAsync ( msg ?? throw new NullReferenceException ( "msg.Text" ) )
48- . ContinueWith (
49- task =>
50- {
51- var ex = task . Exception ;
52- if ( ex is not null )
53- service . LogError (
54- "[SatoriEventClient] ProcessMessage 处理消息时出现异常:" ,
55- ex
56- ) ;
57- } ,
58- TaskContinuationOptions . OnlyOnFaulted
59- ) ;
60- }
61- catch ( Exception e )
62- {
63- service . LogError ( "[SatoriEventClient] 处理消息时出现异常:" , e ) ;
64- }
65- } ) ;
66- _client . DisconnectionHappened . Subscribe ( info =>
41+ await ProcessMessageAsync ( msg ?? throw new NullReferenceException ( "msg.Text" ) ) ;
42+ }
43+ catch ( Exception ex )
44+ {
45+ service . LogError ( "[SatoriEventClient] ProcessMessage 处理消息时出现异常:" , ex ) ;
46+ }
47+ } ;
48+ // 订阅断开连接事件
49+ _client . OnClosed += info =>
6750 {
6851 service . Log (
6952 "[SatoriEventClient] Disconnection Happened. Type:"
7053 + info . Type
7154 + " Description:"
72- + info . CloseStatusDescription
55+ + info . Reason
7356 ) ;
7457 _pingTimer ? . Stop ( ) ;
75- } ) ;
76- _client . ReconnectionHappened . Subscribe ( info =>
58+ } ;
59+
60+ // 订阅重连事件
61+ _client . OnConnected += info =>
7762 {
78- service . Log ( "[SatoriEventClient] Reconnection Happened " + info . Type ) ;
63+ service . Log ( "[SatoriEventClient] Reconnection Happened " + info . IsReconnect ) ;
7964 var identify = new Signal < IdentifySignalBody > //鉴权
8065 {
8166 Op = SignalOperation . Identify ,
82- Body = new IdentifySignalBody { Token = token } ,
67+ Body = new ( ) { Token = token } ,
8368 } ;
8469 SendSignal ( identify ) ;
8570 _pingTimer ? . Start ( ) ;
86- } ) ;
71+ } ;
8772
88- _pingTimer = new Timer
73+ _pingTimer = new ( )
8974 {
9075 AutoReset = true ,
9176 Interval = TimeSpan . FromSeconds ( 10 ) . TotalMilliseconds ,
9277 } ;
9378 _pingTimer . Elapsed += ( _ , _ ) => SendSignal ( new Signal { Op = SignalOperation . Ping } ) ;
9479 }
9580
96- private Task ProcessMessageAsync ( string message )
81+ private ValueTask ProcessMessageAsync ( JToken json )
9782 {
9883 try
9984 {
10085 //_service.LogDebug($"WebSocket::Process {message}");
101- var json = JObject . Parse ( message ) ;
10286 var op = ( SignalOperation ) json . Value < int > ( "op" ) ;
10387 switch ( op )
10488 {
@@ -169,7 +153,7 @@ out senderId
169153 }
170154 }
171155 yield return new CommonCommandReader . ReaderReply (
172- new CommandReader . ReplyInfo (
156+ new (
173157 messageId : id ,
174158 seqId : messageSeq ,
175159 senderId : senderId ,
@@ -203,7 +187,7 @@ out senderId
203187 }
204188 }
205189 _service . Events . CallOnGroupMessageReceived (
206- new GroupMessageEventArgs (
190+ new (
207191 ( ) => new DefaultCommandReader ( Parse ( ) ) ,
208192 ( ) => ValueTask . FromResult ( groupName ?? string . Empty )
209193 )
@@ -213,7 +197,7 @@ out senderId
213197 GroupId = groupId ,
214198 SenderId = senderId ,
215199 SenderMemberCard = name ?? string . Empty ,
216- TextMessageLazy = new Lazy < string > ( ( ) => msg . Content ) ,
200+ TextMessageLazy = new ( ( ) => msg . Content ) ,
217201 Service = _service ,
218202 }
219203 ) ;
@@ -231,7 +215,7 @@ out senderId
231215 $ "{ appName } { account . Status } Features: { string . Join ( "," , account . Features ) } "
232216 ) ;
233217 _service . Events . CallOnBotLogin (
234- new BotLoginEventArgs
218+ new ( )
235219 {
236220 Accounts = _service . AllRobots ,
237221 ClientName = appName ,
@@ -253,7 +237,7 @@ out senderId
253237 _service . Log ( e ) ;
254238 }
255239
256- return Task . CompletedTask ;
240+ return ValueTask . CompletedTask ;
257241 }
258242
259243 private void SendSignal < T > ( T signal )
@@ -263,4 +247,11 @@ private void SendSignal<T>(T signal)
263247 //_service.LogDebug($"WebSocket::SendSignal {text}");
264248 _client . Send ( text ) ;
265249 }
250+
251+ public void Dispose ( )
252+ {
253+ _pingTimer ? . Stop ( ) ;
254+ _pingTimer ? . Dispose ( ) ;
255+ _client ? . Dispose ( ) ;
256+ }
266257}
0 commit comments