void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
        {
            User user = Users.Single(a => a.Connection == sender);

            if (e.Data.Contains("/nick"))
            {
                string[] tmpArray = e.Data.Split(new char[] { ' ' });
                if (tmpArray.Length > 1)
                {
                    string myNewName = tmpArray[1];
                    while (Users.Where(a => a.Name == myNewName).Count() != 0)
                    {
                        myNewName += "_";
                    }
                    if (user.Name != null)
                    {
                        wss.SendToAll("server: '" + user.Name + "' changed name to '" + myNewName + "'");
                    }
                    else
                    {
                        sender.Send("you are now know as '" + myNewName + "'");
                    }
                    user.Name = myNewName;
                }
            }
            else
            {
                string name = (user.Name == null) ? unknownName : user.Name;
                wss.SendToAllExceptOne(name + ": " + e.Data, sender);
                sender.Send("me: " + e.Data);
            }
        }
예제 #2
0
 public void ShouldNotSendOnClosed()
 {
     _connection.Handler = _handlerMock.Object;
     SetupReadLengths(0);
     _connection.StartReceiving();
     _connection.Send("Zing");
     _socketMock.Verify(x => x.Send(It.IsAny <byte[]>(), It.IsAny <Fleck2Extensions.Action>(), It.IsAny <Fleck2Extensions.Action <Exception> >()), Times.Never());
 }
예제 #3
0
 public void Send(String message)
 {
     if (connection != null)
     {
         connection.Send(message);
     }
 }
        public void Register(string roomId, string clientId)
        {
            CheckIfCalledOnValidThread();
            _roomId   = roomId;
            _clientId = clientId;

            if (State != WebSocketConnectionState.Connected)
            {
                Logger.Debug(TAG, $"WebSocket register() in state {State}");
                return;
            }

            var registerMessage = new
            {
                cmd      = "register",
                roomid   = roomId,
                clientid = clientId
            };

            var json = JsonConvert.SerializeObject(registerMessage);


            Logger.Debug(TAG, $"C->WSS: {json}");

            WebSocketConnection.Send(json);

            State = WebSocketConnectionState.Registered;

            foreach (var sendMessage in WsSendQueue)
            {
                Send(sendMessage);
            }

            WsSendQueue.Clear();
        }
예제 #5
0
 void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
 {
     User user = Users.Single(a => a.Connection == sender);
     if (e.Data.Contains("/nick"))
     {
         string[] tmpArray = e.Data.Split(new char[] { ' ' });
         if (tmpArray.Length > 1)
         {
             string myNewName = tmpArray[1];
             while (Users.Where(a => a.Name == myNewName).Count() != 0)
             {
                 myNewName += "_";
             }
             if (user.Name != null)
                 wss.SendToAll("server: '" + user.Name + "' changed name to '" + myNewName + "'");
             else
                 sender.Send("you are now know as '" + myNewName + "'");
             user.Name = myNewName;
         }
     }
     else
     {
         string name = (user.Name == null) ? unknownName : user.Name;
         wss.SendToAllExceptOne(name + ": " + e.Data, sender);
         sender.Send("me: " + e.Data);
     }
 }
        public virtual void Send(string message)
        {
            CheckIfCalledOnValidThread();
            switch (State)
            {
            case WebSocketConnectionState.New:
            case WebSocketConnectionState.Connected:
                Logger.Debug(TAG, $"WS ACC: {message}");
                WsSendQueue.Add(message);
                break;

            case WebSocketConnectionState.Closed:
            case WebSocketConnectionState.Error:
                Logger.Error(TAG, $"WebSocket send() in error or closed state: {message}");
                break;

            case WebSocketConnectionState.Registered:
                message = GetRegisterMessage(message);
                Logger.Debug(TAG, $"C->WSS: {message}");
                WebSocketConnection.Send(message);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #7
0
        private void AbortWithMessageToClient(KPRPCMessage data2client)
        {
            Authorised     = false;
            clientFeatures = null;
            string response = Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client);

            WebSocketConnection.Send(response);
        }
예제 #8
0
 private bool SendDataInternal(object data)
 {
     try
     {
         return(connection.Send(data.SerializeJSON()));
     }
     catch (Exception)
     {
         Dispose();
         return(false);
     }
 }
예제 #9
0
        /// <summary>
        /// Sends the specified signal to the client.
        /// </summary>
        /// <param name="signal">The signal.</param>
        public void Signal(KeePassRPC.DataExchangeModel.Signal signal, string methodName)
        {
            try
            {
                Jayrock.Json.JsonObject call = new Jayrock.Json.JsonObject();
                call["id"]     = ++_currentCallBackId;
                call["method"] = methodName;
                call["params"] = new int[] { (int)signal };

                StringBuilder sb = new StringBuilder();
                Jayrock.Json.Conversion.JsonConvert.Export(call, sb);
                if (WebSocketConnection != null)
                {
                    KPRPCMessage data2client = new KPRPCMessage();
                    data2client.protocol = "jsonrpc";
                    data2client.version  = ProtocolVersion;
                    data2client.jsonrpc  = Encrypt(sb.ToString());

                    // If there was a problem encrypting our message, just abort - the
                    // client won't be able to do anything useful with an error message
                    if (data2client.jsonrpc == null)
                    {
                        if (KPRPC.logger != null)
                        {
                            KPRPC.logger.WriteLine("Encryption error when trying to send signal: " + signal);
                        }
                        return;
                    }

                    // Signalling through the websocket needs to be processed on a different thread becuase handling the incoming messages results in a lock on the list of known connections (which also happens before this Signal function is called) so we want to process this as quickly as possible and avoid deadlocks.
                    // Not sure why this doesn't happen on the standard network port implementation in previous versions (maybe the networking stack created threads automatically before passing on the incoming messages?)

                    //TODO1.3: Does this work in .NET 2?
                    // Respond to each message on a different thread
                    ThreadStart work = delegate
                    {
                        WebSocketConnection.Send(Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client));
                    };
                    Thread messageHandler = new Thread(work);
                    messageHandler.Name = "signalDispatcher";
                    messageHandler.Start();
                }
                else
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
                    this.ConnectionStreamWrite(bytes);
                }
            }
            catch (System.IO.IOException)
            {
                // Sometimes a connection is unexpectedly closed e.g. by Firefox
                // or (more likely) dodgy security "protection". From one year's
                // worth of bug reports (35) 100% of unexpected application
                // exceptions were IOExceptions.
                //
                // We will now ignore this type of exception and allow KeeFox to
                // re-establish the link to KeePass as part of its regular polling loop.
                //
                // The requested KPRPC signal will never be recieved by KeeFox
                // but this should be OK in practice becuase KeeFox will
                // re-establish the relevant state information as soon as it reconnects.
                //
                // BUT: the exception to this rule is when KeeFox fails to receive the
                // "shutdown" signal - it then gets itself in an inconsistent state
                // and has no opportunity to recover until KeePass is running again.
                return;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ERROR! Please click on this box, press CTRL-C on your keyboard and paste into a new post on the KeeFox forum (http://keefox.org/help/forum). Doing this will help other people to use KeeFox without any unexpected error messages like this. Please briefly describe what you were doing when the problem occurred, which version of KeeFox, KeePass and Firefox you use and what other security software you run on your machine. Thanks! Technical detail follows: " + ex.ToString());
            }
        }
예제 #10
0
 public void Send(OutgoingEvent Event)
 {
     WebSocketConnection.Send(JsonConvert.SerializeObject(Event.Body));
     WebSocketManager.LogMessage(Uuid, Event.ToString(), false);
 }