public WebSocketContent(IWebSocketMessage message, WebSocketOpcode opcode)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (message == null) throw new ArgumentNullException("opcode");

            _message = new WebSocketMessage(opcode);
        }
        /// <summary>
        ///     Are about to send a new message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>
        ///     Can be used to prepare the next message. for instance serialize it etc.
        /// </remarks>
        /// <exception cref="NotSupportedException">Message is of a type that the encoder cannot handle.</exception>
        public void Prepare(object message)
        {
            if (message is IWebSocketMessage)
            {
                _message = (IWebSocketMessage)message;
                _message.Payload.Position = 0;
                _totalAmountToSend = (int)_message.Payload.Length;
            }
            else
            {
                try
                {
                    _httpMessageEncoder.Prepare(message);

                    var httpMessage = message as IHttpMessage;
                    if (WebSocketUtils.IsWebSocketUpgrade(httpMessage))
                    {
                        _handshake = httpMessage;
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("This encoder only supports messages deriving from 'HttpMessage' or 'WebSocketMessage'", e);
                }
            }
        }
        /// <summary>
        ///     Are about to send a new message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>
        ///     Can be used to prepare the next message. for instance serialize it etc.
        /// </remarks>
        /// <exception cref="NotSupportedException">Message is of a type that the encoder cannot handle.</exception>
        public void Prepare(object message)
        {
            if (message is IWebSocketMessage)
            {
                _message = (IWebSocketMessage)message;
                _message.Payload.Position = 0;
                _totalAmountToSend        = (int)_message.Payload.Length;
            }
            else
            {
                try
                {
                    _httpMessageEncoder.Prepare(message);

                    var httpMessage = message as IHttpMessage;
                    if (WebSocketUtils.IsWebSocketUpgrade(httpMessage))
                    {
                        _handshake = httpMessage;
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("This encoder only supports messages deriving from 'HttpMessage' or 'WebSocketMessage'", e);
                }
            }
        }
Ejemplo n.º 4
0
        public void big_message()
        {
            IWebSocketMessage actual = null;

            var buffer1 = new SocketBufferFake();

            buffer1.Buffer           = (new byte[] { 2, 126, 255, 255 }).Concat(new byte[WebSocketFrame.FragmentLength]).ToArray(); // continuation frame
            buffer1.BytesTransferred = buffer1.Buffer.Length;

            var buffer2 = new SocketBufferFake();

            buffer2.Buffer           = new byte[] { 128, 1, 1 }; // final frame
            buffer2.BytesTransferred = buffer2.Buffer.Length;

            var decoder = new WebSocketDecoder();

            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer1);
            decoder.ProcessReadBytes(buffer2);

            actual.Opcode.Should().Be(WebSocketOpcode.Binary);
            actual.Payload.Length.Should().Be(WebSocketFrame.FragmentLength + 1);
            actual.Payload.Position = WebSocketFrame.FragmentLength;
            actual.Payload.ReadByte().Should().Be(1);
        }
Ejemplo n.º 5
0
        protected virtual void OnMessageReceived(IWebSocketMessage message)
        {
            var handler = this.MessageReceived;

            if (handler != null)
            {
                handler(message);
            }
        }
Ejemplo n.º 6
0
        protected void QueueMessage(IWebSocketMessage message)
        {
            string json = JsonConvert.SerializeObject(new WebSocketMessage(message));

            if (Logger.Level >= LogSeverity.Debug)
            {
                Logger.Debug($"Out: {json}");
            }
            _engine.QueueMessage(json);
        }
        /// <summary>
        /// Reset encoder state for a new HTTP request.
        /// </summary>
        public void Clear()
        {
            _httpMessageEncoder.Clear();

            if (_message != null && _message.Payload != null)
            {
                _message.Payload.Dispose();
            }

            _bytesToSend = 0;
            _message     = null;
        }
Ejemplo n.º 8
0
        public WebSocketContent(IWebSocketMessage message, WebSocketOpcode opcode)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (message == null)
            {
                throw new ArgumentNullException("opcode");
            }

            _message = new WebSocketMessage(opcode);
        }
Ejemplo n.º 9
0
        public void close_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();

            buffer.Buffer           = new byte[] { 136, 0 };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();

            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Close);
            actual.Payload.Length.Should().Be(0);
        }
Ejemplo n.º 10
0
        private void client_MessageReceived(IWebSocketMessage obj)
        {
            //System.Diagnostics.Debug.WriteLine("Connection.client_MessageReceived " + obj);

            if (!obj.IsComplete)
            {
                return;
            }

            Task.Factory.StartNew(() =>
            {
                var ev = OnMessageReceived;
                if (ev != null)
                {
                    ev(obj.ToString());
                }
            });
        }
Ejemplo n.º 11
0
        public void binary_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();

            buffer.Buffer           = new byte[] { 130, 3, 1, 2, 3 };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();

            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Binary);
            actual.Payload.Length.Should().Be(3);
            actual.Payload.ReadByte().Should().Be(1);
            actual.Payload.ReadByte().Should().Be(2);
            actual.Payload.ReadByte().Should().Be(3);
        }
Ejemplo n.º 12
0
        public void text_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();

            buffer.Buffer           = new byte[] { 129, 3, (byte)'a', (byte)'b', (byte)'c' };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();

            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Text);
            actual.Payload.Length.Should().Be(3);
            actual.Payload.ReadByte().Should().Be((int)'a');
            actual.Payload.ReadByte().Should().Be((int)'b');
            actual.Payload.ReadByte().Should().Be((int)'c');
        }
Ejemplo n.º 13
0
 protected void QueueMessage(IWebSocketMessage message)
 {
     string json = JsonConvert.SerializeObject(new WebSocketMessage(message));
     _engine.QueueMessage(json);
 }
Ejemplo n.º 14
0
 private void websocket_MessageReceived(IWebSocketMessage obj)
 {
     Debug.WriteLine(obj.ToString());
     mlbl.Text = obj.ToString();
 }
Ejemplo n.º 15
0
 private WebSocketMessageEventArgs(IWebSocketMessage message)
 => this.Message = message;
Ejemplo n.º 16
0
        void OnWebsocketReceived(IWebSocketMessage message)
        {

            string msgstr = message.ToString();
            Debug.WriteLine("recevid message:" + msgstr);

            var messageFrame = JsonConvert.DeserializeObject<MessageFrame>(msgstr);
            object typedMessage = new object();

            switch (messageFrame.messageType)
            {
                case "invite":
                    typedMessage = JsonConvert.DeserializeObject<JoinMessage>(messageFrame.data.ToString());
                    break;
                case "echo-request":
                    typedMessage = JsonConvert.DeserializeObject<EchoWithTimestamp>(messageFrame.data.ToString());
                    break;
                case "echo-reply":
                    typedMessage = JsonConvert.DeserializeObject<EchoWithTimestamp>(messageFrame.data.ToString());
                    break;
                default:
                    throw new Exception("invalid message type");
            }

            Debug.WriteLine(typedMessage.ToString() + " received ");
            //numberOfReceivedMessages++;
            Received(msgstr);
        }
        /// <summary>
        /// Reset encoder state for a new HTTP request.
        /// </summary>
        public void Clear()
        {
            _httpMessageEncoder.Clear();

            if (_message != null && _message.Payload != null)
                _message.Payload.Dispose();

            _bytesToSend = 0;
            _message = null;
        }
Ejemplo n.º 18
0
 private void websocket_MessageReceived(IWebSocketMessage obj)
 {
     Debug.WriteLine(obj.ToString());
     mlbl.Text = obj.ToString();
 }
Ejemplo n.º 19
0
		protected void QueueMessage(IWebSocketMessage message)
		{
			string json = JsonConvert.SerializeObject(new WebSocketMessage(message));
			if (Logger.Level >= LogSeverity.Debug)
                Logger.Debug( $"Out: {json}");
			_engine.QueueMessage(json);
		}
Ejemplo n.º 20
0
 public WebSocketMessage(IWebSocketMessage msg)
 {
     Operation = msg.OpCode;
     Payload   = msg.Payload;
 }
Ejemplo n.º 21
0
 void websocket_MessageReceived(IWebSocketMessage obj)
 {
     Debug.WriteLine(obj.ToString());
     counter++;
 }
Ejemplo n.º 22
0
		private async void websocket_MessageReceived(IWebSocketMessage e)
		{
			
			Debug.WriteLine ("websocket_MessageReceived \n {0}", e.ToString());

			try
			{
				RootObject cmd = JsonConvert.DeserializeObject<RootObject>(e.ToString());

				if(cmd.cmd_db_allnode != null){

					Debug.WriteLine("node_change_detected");
					foreach(var data in cmd.cmd_db_allnode)
					{
						bool isUpdate = false;
						try
						{
							await App.Database.Save_DBAllNode_Item(data);							
						}
						catch (Exception exx)
						{
							isUpdate = true;
							Debug.WriteLine("Exception {0}", exx.ToString());
						}

						if (isUpdate)
						{
							await App.Database.Update_DBAllNode_All_Item(data);
						}
					}

					foreach(var data in await App.Database.GetItems())
					{
						#region io_value

						#endregion
						var ioNUmber = 0;
						if(data.node_deviceType.Equals(EnumtoString.EnumString(NodeDeviceType.InWallSwitch))){
							ioNUmber = 2;
						}else if(data.node_deviceType.Equals(EnumtoString.EnumString(NodeDeviceType.GeneralPurposeDetector))){
							ioNUmber = 4;
						}else{
							ioNUmber = 1;
						}

						try
						{							
							for(var i = 0;i < ioNUmber;i++)
							{
								string io_state = find_io_value(i, data.node_io);
								await App.Database.Update_NameByUser_ioValue2(data.node_io, io_state, data.node_addr, i.ToString());
								if(!data.node_deviceType.Equals(EnumtoString.EnumString(NodeDeviceType.UnknowDeviceType))){
									await App.Database.Save_NameByUser(data, i.ToString(), i.ToString(), io_state);
								}

							}
						}
						catch (Exception exx)
						{
							Debug.WriteLine("Exception2 {0}", exx.ToString());
						}
					}
					foreach(var i in await App.Database.GetItems())
					{
						//Log.Info ("From Database" , i.node_addr);
						//Log.Info ("From node_status" , i.node_status);
					}

					foreach(var i in await App.Database.Get_NameByUser())
					{
						//System.Diagnostics.Debug.WriteLine("=====> {0}, {1}, {2}", i.node_addr, i.io_name_by_user, i.target_io);
						//Log.Info ("From Get_NameByUser" , String.Format("=====> {0}, ->{1}<-, {2}, {3}, {4}, {5}", i.node_addr, i.node_name_by_user, i.node_io, i.io_name_by_user, i.node_io_p, i.io_value));
						Debug.WriteLine("=====> {0}, ->{1}<-, {2}, {3}, {4}, {5}", i.node_addr, i.node_name_by_user, i.node_io, i.io_name_by_user, i.node_io_p, i.io_value);
						//Log.Info ("From Get_NameByUser" , i.node_name_by_user);
						//Log.Info ("From Get_NameByUser" , i.io_name_by_user);
						//Log.Info ("From Get_NameByUser" , i.target_io);
					}

					var node_command = cmd.cmd_db_allnode[0].node_command;

					if(node_command.Equals("io_change_detected")){
						
						if(cmd.cmd_db_allnode[0].node_deviceType.Equals(EnumtoString.EnumString(NodeDeviceType.InWallSwitch))){
							Debug.WriteLine ("io_change_detectedkk" , "kk");
							MessagingCenter.Send<Node_io_ItemPage, string> (new Node_io_ItemPage(), "Node_io_ItemPage_Io_Change", cmd.cmd_db_allnode[0].node_addr);

							/**new System.Threading.Tasks.Task (() => {
								Device.BeginInvokeOnMainThread ( async () => {									

								});
							}).Start();**/

						}else if(cmd.cmd_db_allnode[0].node_deviceType.Equals(EnumtoString.EnumString(NodeDeviceType.GeneralPurposeDetector))){

							Debug.WriteLine ("GPD_io_Change");

							MessagingCenter.Send<Node_io_GpdPage, string> (new Node_io_GpdPage(), "Node_io_GpdPage_Io_Change", cmd.cmd_db_allnode[0].node_addr);

							/**var profileStatus = await App.Database.Get_Profile_Name_Is_Open("True");

							foreach(var data in profileStatus)
							{
								foreach(var item in await App.Database.Get_Profile_IO_Data_By_Addr(cmd.cmd_db_allnode[0].node_addr, data.profileName))
								{										
									if(item.alert_mode.Equals("True"))
									{
										foreach(var nodeName in await App.Database.Get_NameByUser_by_addr(item.node_addr))
										{
											showNotify(nodeName.node_name_by_user, item.node_addr);
											break;
										}
									}
									break;
								}
							}**/

						}else{
							
							Debug.WriteLine ("aaaaaaaaaaa");
						}

					}else if(node_command.Equals("db_allnode")){

						Debug.WriteLine("MessageReceived4  db_allnode");
						loadingDialog.Hide();
						//App.current.MainPage = new MenuTabPage();
						await Navigation.PushModalAsync(new MenuTabPage());

					}

				}else if(cmd.cmd_login != null){

					#region cmd_login
					Debug.WriteLine("cmd_login");
					#endregion

					foreach(var data in cmd.cmd_login)
					{
						if(data.flagForLogin.Equals("pass") && data.username.Equals(username.Text)){
							#region testws2
							await ws_client.SendAsync ("{\"cmd_db_allnode\":[{\"node_type\":\"0x3ff20\",\"node_addr\":\"[00:13:a2:00:40:ad:58:rm]!\",\"node_status\":\"0\",\"node_io\":\"FC\",\"node_command\":\"db_allnode\"},{\"node_type\":\"0x3ff01\",\"node_addr\":\"[00:13:a2:00:40:ad:58:ab]!\",\"node_status\":\"0\",\"node_io\":\"FE\",\"node_command\":\"db_allnode\"}, {\"node_type\":\"0x3ff01\",\"node_addr\":\"[00:13:a2:00:40:ad:58:ae]!\",\"node_status\":\"0\",\"node_io\":\"FE\",\"node_command\":\"db_allnode\"},{\"node_type\":\"0x3ff11\",\"node_addr\":\"[00:13:a2:00:40:ad:58:kk]!\",\"node_status\":\"0\",\"node_io\":\"F8\",\"node_command\":\"db_allnode\"},{\"node_type\":\"0x3ff11\",\"node_addr\":\"[00:13:a2:00:40:b2:16:5a]!\",\"node_status\":\"0\",\"node_io\":\"FE\",\"node_command\":\"db_allnode\"},{\"node_type\":\"0xa001a\",\"node_addr\":\"[00:13:a2:00:40:ad:57:e3]!\",\"node_status\":\"0\",\"node_io\":\"FA\",\"node_command\":\"db_allnode\"}, {\"node_type\":\"0xa001c\",\"node_addr\":\"[00:13:a2:00:40:ad:57:ca]!\",\"node_status\":\"0\",\"node_io\":\"FA\",\"node_command\":\"db_allnode\"}]}");
							#endregion
							////no websocketMaster.Send("{\"cmd_login\":[{\"flagForLogin\":\"pass\",\"lastConnectWebscoketUrl\":\"ws://echo.websocket.org\"}]})");

							await App.Database.Save_Login_Item (username.Text, password.Text, data.flagForLogin, data.lastConnectWebscoketUrl);
							#region FirstSendToGateway
							Db_allnode db = new Db_allnode ();
							db.node_command = "db_allnode";
							db.ID = 0;
							db.nodeStatusToString = "";
							db.node_addr = "";
							db.node_deviceType = "";
							db.node_io = "";
							db.node_status = "";
							db.node_type = "";
							db.node_io_p = "";

							var FirstSend = JsonConvert.SerializeObject(db);
							await ws_client.SendAsync (FirstSend);

							#endregion

						}else if (data.flagForLogin.Equals("not_pass")){

							Device.BeginInvokeOnMainThread (async () => {
								UserDialogs.Instance.ErrorToast("Waring","Username or Password is Invalid");
								//LoginPage.loginButton.IsEnabled = true;
								//LoginPage.logoutButton.IsEnabled = true;
								//LoginPage.activityIndicator.IsRunning = false;
								//LoginPage.loginFail.IsVisible = true;
								//MessageBarManager.SharedInstance.ShowMessage("Waring", "Username or Password is Invalid", MessageType.Error);
							});
							await ws_client.CloseAsync();

						}else if (data.flagForLogin.Equals("add_user_success")){
							await App.Database.Add_Login_Username_Show_For_Del(data.username);

							Device.BeginInvokeOnMainThread (async () => {

								//MessageBarManager.SharedInstance.ShowMessage("Account", data.username + " Added" , MessageType.Success);
							});
						}else if (data.flagForLogin.Equals("user_exits")){
							Device.BeginInvokeOnMainThread (async () => {

								//MessageBarManager.SharedInstance.ShowMessage("Account", data.username + " Already in use" , MessageType.Error);
							});

						}else if (data.flagForLogin.Equals("user_password_change")){
							foreach (var LoginData in await App.Database.Get_flag_Login()) //check wa koiy login? 
							{
								await App.Database.Delete_Login_Item ();
								//await App.Database.Save_Login_Item(LoginData.username, Change_Password_Page.newPassword.Text, LoginData.flagForLogin, LoginData.lastConnectWebscoketUrl);
								break;
							}
							Device.BeginInvokeOnMainThread (async () => {

								//MessageBarManager.SharedInstance.ShowMessage("Password", "\"New Password is Changed" , MessageType.Success);
							});
						}else if (data.flagForLogin.Equals("query_user")){

							await App.Database.Add_Login_Username_Show_For_Del(data.username);

						}else if (data.flagForLogin.Equals("user_deleted")){

							await App.Database.Delete_Login_Username_Show_For_Del(data.username);
							Device.BeginInvokeOnMainThread (async () => {
								//Admin_Delete_User_Page.usernameForDelete.ItemsSource = await App.Database.Get_Login_Username_Show_For_Del();
								//MessageBarManager.SharedInstance.ShowMessage("Account", data.username + " is Deleted" , MessageType.Success);
							});	
						}
					}

				}else if(cmd.cmd_remote != null){

					foreach(var data in cmd.cmd_remote)
					{
						//Log.Info ("cmd_remote" , "cmd_remote");

						foreach (var checkUser in await App.Database.Get_flag_Login())  
						{
							if(checkUser.username == data.remote_username){
								if(data.node_command.Equals("remote_code_success")){
									
									await App.Database.Save_RemoteData_Item(data.node_addr, data.remote_button_name, data.remote_username);
									MessagingCenter.Send<Add_Remote_Button> (new Add_Remote_Button(), "remote_code_success");

								}else if(data.node_command.Equals("remote_code_sync_database")){

									await App.Database.Save_RemoteData_Item(data.node_addr, data.remote_button_name, data.remote_username);

								}else if(data.node_command.Equals("remote_code_fail")){

									UserDialogs.Instance.ShowError("Try again..");

								}else if(data.node_command.Equals("name_exist")){

									UserDialogs.Instance.ShowError("Name_exist..");

								}else if(data.node_command.Equals("remote_send_success")){

									Device.BeginInvokeOnMainThread (async () => {
										//var notificator = DependencyService.Get<IToastNotificator>();
										//await notificator.Notify(ToastNotificationType.Success, 
										//	"Success", " Remote code success", TimeSpan.FromSeconds(1));
									});

								}else if(data.node_command.Equals("delete_remote_success")){
									
									await App.Database.Delete_RemoteData_Custom_Item(data.remote_button_name, data.remote_username);
									MessagingCenter.Send<RemotePage> (new RemotePage(), "delete_remote_success");

								}else if(data.node_command.Equals("rename_remote_success")){
									
									await App.Database.Rename_RemoteData_Item(data.remote_button_name, data.new_button_name, data.remote_username);
									MessagingCenter.Send<RemotePage> (new RemotePage(), "rename_remote_success");

								}
							}
							break;
						}


					}
				}
			}
			catch(Exception ex)
			{
				Debug.WriteLine ("websocket_MessageReceived Exception\n {0}", ex);
			}
		}
Ejemplo n.º 23
0
        protected void QueueMessage(IWebSocketMessage message)
        {
            string json = JsonConvert.SerializeObject(new WebSocketMessage(message));

            _engine.QueueMessage(json);
        }
Ejemplo n.º 24
0
 void websocket_MessageReceived(IWebSocketMessage obj)
 {
     Debug.WriteLine(obj.ToString());
     counter++;
 }
Ejemplo n.º 25
0
 public WebSocketMessage(IWebSocketMessage msg)
 {
     Operation = msg.OpCode;
     Payload = msg.Payload;
 }
Ejemplo n.º 26
0
		private void OnMessage(IWebSocketMessage obj)
		{
			Output.Add(new SocketMessage{ Content = obj.ToString()});
		}