public async Task CloseAsync(WebNotifyConnection client) { if (client != null) { await client.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Internal Server Close.", CancellationToken.None); Remove(client); } }
private void Remove(WebNotifyConnection client) { try { connections.Remove(client); client?.Dispose(); } catch (Exception ex) { Helper.OnException(ex); } RemoveClient?.Invoke(this, new WebNotifyEventArgs(client)); }
private void OnMessageReceive(WebNotifyConnection client, MemoryStream stream) { stream.Position = 0; var property = (string)null; var type = (Type)null; var serializer = JsonSerializer.Create(jsonSettings); using (var reader = new StreamReader(stream)) using (var jreader = new JsonTextReader(reader)) { while (jreader.Read()) { switch (jreader.TokenType) { case JsonToken.PropertyName: property = (string)jreader.Value; break; case JsonToken.String: switch (property) { case ("Type"): type = TypeHelper.ParseType((string)jreader.Value); break; } break; case JsonToken.StartObject: if (property == "Value" && type != null) { property = null; var obj = serializer.Deserialize(jreader, type); if (obj is WebNotifyRegistration data) { client.Platform = data.Platform; client.Application = data.Application; client.Version = data.Version; return; } } break; } } } var message = Encoding.UTF8.GetString(stream.ToArray()); ReceiveMessage?.Invoke(this, new WebNotifyEventArgs(client, message)); }
public void Register(WebSocket socket, User user, string address) { var client = GetBySocket(socket); if (client == null) { client = new WebNotifyConnection { Socket = socket, User = user, Address = address, }; connections.Add(client); } }
public WebNotifyEventArgs(WebNotifyConnection client, string message = null) { Client = client; Message = message; }