1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Net . Http ;
4+ using System . Threading ;
15using System . Threading . Tasks ;
2- using SocketIOClient . Transport . Http ;
6+ using SocketIOClient . Core . Messages ;
7+ using SocketIOClient . Serializer . Decapsulation ;
8+ using SocketIOClient . V2 . Protocol . Http ;
9+ using SocketIOClient . V2 . Serializer . SystemTextJson ;
310using SocketIOClient . V2 . Session ;
11+ using SocketIOClient . V2 . Session . EngineIOHttpAdapter ;
12+ using SocketIOClient . V2 . UriConverter ;
13+ using IHttpClient = SocketIOClient . Transport . Http . IHttpClient ;
414
515namespace SocketIOClient . V2 ;
616
717public class SocketIO : ISocketIO
818{
919 public IHttpClient HttpClient { get ; set ; }
10- public ISession Session { get ; set ; }
20+ public ISessionFactory SessionFactory { get ; set ; }
21+ private ISession _session ;
22+ public int PacketId { get ; private set ; }
23+ public bool Connected { get ; private set ; }
24+
25+
26+ private readonly Dictionary < int , Action < IAckMessage > > _ackHandlers = new ( ) ;
27+ private readonly Dictionary < int , Func < SocketIOResponse , Task > > _funcHandlers = new ( ) ;
28+ private readonly SocketIOOptions _options ;
29+
30+
31+ public SocketIO ( Uri uri , SocketIOOptions options )
32+ {
33+ _options = options ;
34+ SessionFactory = new DefaultSessionFactory ( ) ;
35+ }
36+
37+ public SocketIO ( Uri uri ) : this ( uri , new SocketIOOptions ( ) )
38+ {
39+ }
40+
41+ public SocketIO ( string uri ) : this ( new Uri ( uri ) , new SocketIOOptions ( ) )
42+ {
43+ }
44+
45+ private IEngineIOAdapter NewEnginIOAdapter ( )
46+ {
47+ if ( _options . EIO == EngineIO . V3 )
48+ {
49+ return new EngineIO3Adapter ( ) ;
50+ }
51+ return new EngineIO4Adapter ( ) ;
52+ }
1153
1254 public Task ConnectAsync ( )
1355 {
14- throw new System . NotImplementedException ( ) ;
56+ _session = SessionFactory . New ( _options . EIO ) ;
57+ // Session.Subscribe(this);
58+ Connected = true ;
59+ return Task . CompletedTask ;
60+ }
61+
62+ // public Task EmitAsync(string eventName, Action ack)
63+ // {
64+ // throw new NotImplementedException();
65+ // }
66+
67+ private void ThrowIfNotConnected ( )
68+ {
69+ if ( Connected )
70+ {
71+ return ;
72+ }
73+ throw new InvalidOperationException ( "SocketIO is not connected." ) ;
74+ }
75+
76+ public async Task EmitAsync ( string eventName , Action < IAckMessage > ack )
77+ {
78+ ThrowIfNotConnected ( ) ;
79+ PacketId ++ ;
80+ await _session . SendAsync ( [ eventName ] , CancellationToken . None ) ;
81+ _ackHandlers . Add ( PacketId , ack ) ;
1582 }
1683
17- public Task EmitAsync ( string eventName )
84+ public void OnNext ( IMessage message )
1885 {
19- throw new System . NotImplementedException ( ) ;
86+ if ( message . Type == MessageType . Ack )
87+ {
88+ var ackMessage = ( IAckMessage ) message ;
89+ _ackHandlers [ ackMessage . Id ] ( ackMessage ) ;
90+ }
2091 }
2192}
0 commit comments