public static async Task JoinRoomAsync(this SocketIoClient client, string roomName) { using var called = client.Emit("join", roomName).SubscribeCalled(e => { Assert.Equal("joined", e.Data[0].ToString()); }); await called.AssertOnceAsync(TimeSpan.FromMilliseconds(100)); }
public IObservable <Dictionary <string, object> > Connect( IObservable <string> input, ICollection <IMessageUnpacker> unpackers) { return(Observable.Defer(() => Observable.Create <Dictionary <string, object> >(async(sink, token) => { SocketIoClient socket = new SocketIoClient(); Uri connectionUri = null; try { connectionUri = new Uri("https://streamer.cryptocompare.com"); await socket.ConnectAsync(connectionUri); _logger.LogInformation("[EXTERNAL-SERVICE] Connecting to CryptoCompare.com ..."); } catch (UriFormatException e) { sink.OnError(e); return; } Func <Task> closeSocket = async() => { await socket.DisconnectAsync(); _logger.LogInformation("[EXTERNAL-SERVICE] Connection to CryptoCompare.com closed"); }; socket.Connected += (sender, args) => { input.Subscribe(v => { string[] subscription = { v }; Dictionary <string, object> subs = new Dictionary <string, object>(); subs.Add("subs", subscription); socket.Emit("SubAdd", subs); }, onError: e => sink.OnError(e)); }; socket.On("m", async args => { string message = args; string messageType = message.Substring(0, message.IndexOf("~")); foreach (IMessageUnpacker unpacker in unpackers) { if (unpacker.Supports(messageType)) { try { sink.OnNext(unpacker.Unpack(message)); } catch (Exception e) { sink.OnError(e); await closeSocket.Invoke(); } break; } } }); socket.ErrorReceived += (sender, args) => sink.OnError(new Exception(args.Value)); socket.Disconnected += (sender, args) => sink.OnCompleted(); token.Register(async() => await closeSocket()); await socket.ConnectAsync(connectionUri); }))); }
public void SendMessageServer(string message) { Console.WriteLine("Emiting message to the server..."); socket.Emit(RequestEventName, message); }
public async Task NoAcksOrHandlerNotifications() { // Arrange var serializer = new NewtonSoftJsonSerializer(); var logger = new NullLogger <SocketIoClient>(); var opts = new SocketIoClientOptions( serializer, logger, Encoding.UTF8 ); var client = new SocketIoClient(opts); client.Events.OnConnect.Subscribe( args => Log($"[1] Connected: {args}")); client.Events.OnPacket.Subscribe(packet => { Log($"[1] Received packet: {packet}"); }); string clientId1 = null; client.Events.OnHandshake.Subscribe(res => { clientId1 = res.Sid; }); // Act await client.OpenAsync(new Uri("http://localhost:3300")); var data1 = new Payload() { Id = 42, Name = "Douglas Adams" }; var data2 = new Payload() { Id = 86, Name = "Carl Sagan" }; while (clientId1 is null) { Thread.Sleep(50); } client.On("/consume") .Subscribe(message => { Log($"Received notification: {message.EventName}, data: {message.Data}"); }); Log($"- subscribe to 'test' topic {clientId1} -"); client.Emit("/subscribe", new SubscribeRequest( clientId1, "test", 0, 10, 0 ) ).Subscribe(ack => { Log($"subscribe ack: {ack}"); }); Log("- about to publish - "); client.Emit("/publish", new PublishRequest { Topic = "test", Channel = 1, Message = JsonConvert.SerializeObject(data1) } ).Subscribe(ack => { Log($"publish ack: {JsonConvert.SerializeObject(ack)}"); }); client.Emit("/publish", new PublishRequest { Topic = "test", Channel = 1, Message = JsonConvert.SerializeObject(data1) } ).Subscribe(ack => { Log($"publish ack: {JsonConvert.SerializeObject(ack)}"); }); // Assert Log("- waiting for 2s just in case -"); await Task.Delay(2000); Log("- test ends -"); }