void MessageHandle() { while (true) { lock (client.ReceivedMessages) { foreach (var message in client.ReceivedMessages) { var messageHandler = MessageHandlerFactory.GetMessageHandler(message.MessageId); if (messageHandler != null) { messageHandler.Execute(this, message); } } client.ReceivedMessages.Clear(); } } }
public override void Run() { var mainArgs = new CefMainArgs(new string[] { }); var app = new OffscreenApp(); CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); // init CEF var settings = new CefSettings { SingleProcess = false, MultiThreadedMessageLoop = true, LogSeverity = CefLogSeverity.Error, LogFile = "CefGlue.log" }; CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero); Trace.WriteLine("Starting processing of messages"); // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump. Client.OnMessage((receivedMessage) => { try { // Process the message Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString()); var handler = MessageHandlerFactory.GetMessageHandler(receivedMessage.ContentType); handler.Handle(receivedMessage, CompletedClient); receivedMessage.Complete(); } catch (Exception ex) { // Handle any message processing specific exceptions here } }); CompletedEvent.WaitOne(); }
public override void OnMessage(string message) { var webSocketMessage = JsonConvert.DeserializeObject <WebSocketMessage>(message); if (webSocketMessage != null) { if (!IsAuthenticated) { var handler = new AuthenticationHandler(SiteId); var result = handler.ProcessMessage(webSocketMessage); IsAuthenticated = handler.IsAuthenticated; Send(JsonConvert.SerializeObject(result)); } else { var webSwitchMessageHandler = MessageHandlerFactory.GetMessageHandler(webSocketMessage.Type, SiteId); var result = webSwitchMessageHandler.ProcessMessage(webSocketMessage); if (result != null) { switch (result.BroadcastType) { case WebSocketBroadcastType.Pi: WebSocketSession.SendToPi(JsonConvert.SerializeObject(result)); break; case WebSocketBroadcastType.Web: WebSocketSession.SendToWeb(JsonConvert.SerializeObject(result)); break; case WebSocketBroadcastType.All: WebSocketSession.Broadcast(JsonConvert.SerializeObject(result)); break; } } } } }
static void ReceiveData() { while (client != null) { try { lock (client) { byte[] data = new byte[1024]; int bytes = client.GetStream().Read(data, 0, data.Length); string responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes); GenericMessage genericMessage = JsonSerializer.Deserialize <GenericMessage>(responseData); IMessage message = MessageFactory.GetMessage(genericMessage.MessageId, responseData); IMessageHandler messageHandler = MessageHandlerFactory.GetMessageHandler(genericMessage.MessageId); messageHandler.Execute(client, message); } } catch (System.IO.IOException) { } catch (System.ObjectDisposedException) { } } }
static void HandleClient(Server server, TcpClient client) { byte[] bytes = new byte[1024]; string data; while (true) { NetworkStream stream = client.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.UTF8.GetString(bytes, 0, i); GenericMessage genericMessage = JsonSerializer.Deserialize <GenericMessage>(data); IMessage message = MessageFactory.GetMessage(genericMessage.MessageId, data); IMessageHandler handler = MessageHandlerFactory.GetMessageHandler(genericMessage.MessageId); handler.Execute(server, client, message); } } }