/// <summary> /// 初始化订阅发送 /// </summary> /// <param name="options"></param> public static void InitializationChannel(WebSocketClientConfig options) { if (_channelInstance == null) { _channelInstance = new WebSocketChannelClient(options); } }
public WebSocketServerBroker(WebSocketServerConfig options) : base(options) { ClusterServer = new ConcurrentDictionary <Guid, ConcurrentDictionary <Guid, WebSocketSession> >(); ChannelClient = new WebSocketChannelClient(options); _server = options.ServerPath; var ServerKey = RedisKeyFormatUtil.GetServerKey(_appId, _server); var OnLineServerKey = RedisKeyFormatUtil.GetOnLineServerKey(_appId); _redis.HSet(OnLineServerKey, _appId, _server); _redis.Subscribe((ServerKey, RedisSubScribleMessage)); }
public static async Task TestChannel(ISubscriptionApi subscriptionApi, string url, Action start) { // Listen for new channels at /test to be created Butterfly.Core.Channel.Channel channelA = null; TestDisposable testDisposableA = new TestDisposable(); Butterfly.Core.Channel.Channel channelB = null; TestDisposable testDisposableB = new TestDisposable(); // Register channel subscriptionApi.OnSubscribe(channelKey: "A", handler: (vars, channel) => { channelA = channel; return(testDisposableA); }); subscriptionApi.OnSubscribe(channelKey: "B", handler: (vars, channel) => { channelB = channel; return(testDisposableB); }); subscriptionApi.Start(); start?.Invoke(); var testAuthId = "123"; // Test creating a channel from the client var channelClient = new WebSocketChannelClient(url, $"Test {testAuthId}", heartbeatEveryMillis: 1000); channelClient.Start(); await Task.Delay(3000); Assert.AreEqual(1, subscriptionApi.AuthenticatedConnections.Count); Assert.IsNotNull(subscriptionApi.GetConnection(testAuthId)); List <string> messageCollectorA = new List <string>(); channelClient.Subscribe(message => { string data = message.GetAs("data", (string)null); messageCollectorA.Add(data); }, channelKey: "A"); await Task.Delay(500); Assert.IsNotNull(channelA); List <string> messageCollectorB = new List <string>(); channelClient.Subscribe(message => { string data = message.GetAs("data", (string)null); messageCollectorB.Add(data); }, channelKey: "B"); await Task.Delay(500); Assert.IsNotNull(channelB); // Test if sending a message on the server is received on the client subscriptionApi.GetConnection(testAuthId, true).QueueMessage(channelKey: channelA.ChannelKey, messageType: "TypeA", data: "HelloA"); await Task.Delay(200); Assert.AreEqual(1, messageCollectorA.Count); Assert.AreEqual(0, messageCollectorB.Count); Assert.AreEqual("HelloA", messageCollectorA[0]); // Test if sending a message on the server is received on the client subscriptionApi.GetConnection(testAuthId, true).QueueMessage(channelKey: channelB.ChannelKey, messageType: "TypeB", data: "HelloB"); await Task.Delay(200); Assert.AreEqual(1, messageCollectorA.Count); Assert.AreEqual(1, messageCollectorB.Count); Assert.AreEqual("HelloB", messageCollectorB[0]); // Test if heartbeats keep the channel alive properly await Task.Delay(3000); Assert.AreEqual(1, subscriptionApi.AuthenticatedConnections.Count); // Test if channel is disposed if it is removed from server channelClient.Dispose(); await Task.Delay(3000); Assert.AreEqual(0, subscriptionApi.AuthenticatedConnections.Count); // Test if the disposable returned by OnNewChannel() was disposed as well Assert.AreEqual(true, testDisposableA.disposed); Assert.AreEqual(true, testDisposableB.disposed); }