Beispiel #1
0
        /// <summary>
        /// The on web socket message.
        /// </summary>
        /// <param name="server">
        /// The server.
        /// </param>
        /// <param name="connectionId">
        /// The connection id.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="dataSize">
        /// The data size.
        /// </param>
        protected override void OnWebSocketMessage(CefServer server, int connectionId, IntPtr data, long dataSize)
        {
            Task.Factory.StartNew(() =>
            {
                IntPtr tempPtr   = IntPtr.Zero;
                IntPtr outIntPtr = IntPtr.Zero;

                try
                {
                    var managedArray = new byte[dataSize];
                    Marshal.Copy(data, managedArray, 0, (int)dataSize);
                    tempPtr = Marshal.AllocHGlobal(managedArray.Length);
                    Marshal.Copy(managedArray, 0, tempPtr, managedArray.Length);

                    var requestString = Encoding.UTF8.GetString(managedArray);

                    var jsonData = JsonMapper.ToObject <JsonData>(requestString);
                    var request  = new ChromelyRequest(jsonData);

                    var response = RequestTaskRunner.Run(request);
                    WebsocketMessageSender.Send(connectionId, response);
                }
                catch (Exception exception)
                {
                    Log.Error(exception);
                }
                finally
                {
                    // Free the unmanaged memory.
                    Marshal.FreeHGlobal(tempPtr);
                    Marshal.FreeHGlobal(outIntPtr);
                }
            });
        }
        /// <summary>
        /// The on message.
        /// </summary>
        /// <param name="connectionId">
        /// The connection id.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="dataSize">
        /// The data size.
        /// </param>
        public void OnMessage(int connectionId, IntPtr data, long dataSize)
        {
            Task.Run(() =>
            {
                IntPtr tempPtr = IntPtr.Zero;

                try
                {
                    var managedArray = new byte[dataSize];
                    Marshal.Copy(data, managedArray, 0, (int)dataSize);
                    tempPtr = Marshal.AllocHGlobal(managedArray.Length);
                    Marshal.Copy(managedArray, 0, tempPtr, managedArray.Length);

                    var requestString = Encoding.UTF8.GetString(managedArray);
                    var requestInfo   = new RequestInfo(requestString);

                    switch (requestInfo.Type)
                    {
                    case MessageType.Echo:
                        WebsocketMessageSender.Send(connectionId, requestInfo.Data);
                        break;

                    case MessageType.TargetRecepient:
                        WebsocketMessageSender.Send(requestInfo.TargetConnectionId, requestInfo.Data);
                        break;

                    case MessageType.Broadcast:
                        WebsocketMessageSender.Broadcast(connectionId, requestInfo.Data);
                        break;

                    case MessageType.ControllerAction:
                        var jsonData = JsonMapper.ToObject <JsonData>(requestString);
                        var request  = new ChromelyRequest(jsonData);
                        var response = RequestTaskRunner.Run(request);
                        WebsocketMessageSender.Send(connectionId, response);
                        break;

                    default:
                        WebsocketMessageSender.Send(connectionId, requestInfo.Data);
                        break;
                    }
                }
                catch (Exception exception)
                {
                    Log.Error(exception);
                }
                finally
                {
                    // Free the unmanaged memory.
                    Marshal.FreeHGlobal(tempPtr);
                }
            });
        }
 /// <summary>
 /// The send messages to client.
 /// </summary>
 private void SendMessagesToClient()
 {
     Task.Run(() =>
     {
         try
         {
             var index = 0;
             while (mReceiveFromServer)
             {
                 var info = mServerSentMessages[index];
                 var data = $"{DateTime.Now}: {info}.";
                 WebsocketMessageSender.Send(mReceiverConnectionId, data);
                 System.Threading.Thread.Sleep(mSecondsDelay * 1000);
                 index++;
                 index = (index >= mServerSentMessages.Count) ? 0 : index;
             }
         }
         catch (Exception exception)
         {
             Log.Error(exception);
         }
     });
 }
 /// <summary>
 /// The send.
 /// </summary>
 /// <param name="connectionId">
 /// The connection id.
 /// </param>
 /// <param name="response">
 /// The response.
 /// </param>
 public void Send(int connectionId, ChromelyResponse response)
 {
     WebsocketMessageSender.Send(connectionId, response);
 }
 /// <summary>
 /// The send.
 /// </summary>
 /// <param name="connectionId">
 /// The connection id.
 /// </param>
 /// <param name="data">
 /// The data.
 /// </param>
 public void Send(int connectionId, string data)
 {
     WebsocketMessageSender.Send(connectionId, data);
 }
 /// <summary>
 /// The send.
 /// </summary>
 /// <param name="connectionId">
 /// The connection id.
 /// </param>
 /// <param name="data">
 /// The data.
 /// </param>
 /// <param name="dataSize">
 /// The data size.
 /// </param>
 public void Send(int connectionId, IntPtr data, long dataSize)
 {
     WebsocketMessageSender.Send(connectionId, data, dataSize);
 }