private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //bool getMsg = false; while (worker.CancellationPending == false) { /*string msg = client.Receive(); * ponerMensaje(msg);*/ //newMsg = client.Receive(); //if (newMsg != "Operation Timeout") //getMsg = true; MensajeServer newMsg = client.Receive(); if (newMsg.type == "status") { } else if (newMsg.type == "message") { dataBase.insert((MensajeServerMensaje)newMsg); } else if (newMsg.type == "error") { } } }
/// <summary> /// Receive data from the server using the established socket connection /// </summary> /// <returns>The data received from the server</returns> public MensajeServer Receive() { //string response = "Operation Timeout"; char comunication = 'r'; //Creamos el mensaje que nos ha mandado el servidor MensajeServer msg = new MensajeServer("NoResponse"); // We are receiving over an established socket connection if (_socket != null) { // Create SocketAsyncEventArgs context object SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint; // Setup the buffer to receive the data socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE); // Inline event handler for the Completed event. // Note: This even handler was implemented inline in order to make // this method self-contained. socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { if (comunication == 'r') { if (e.SocketError == SocketError.Success) { // Retrieve the data from the buffer respuesta = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred); respuesta = respuesta.Trim('\0'); //TODO: CONTINUAR AQUI. if (respuesta != "") { JObject o = JObject.Parse(respuesta); if ((string)o.SelectToken("type") == "message") { msg = new MensajeServerMensaje((string)o.SelectToken("message"), (string)o.SelectToken("room"), (string)o.SelectToken("name"), Convert.ToInt32((string)o.SelectToken("user_id"))); } else if ((string)o.SelectToken("type") == "status") { msg = new MensajeServerStatus((string)o.SelectToken("status")); } else if ((string)o.SelectToken("type") == "error") { msg = new MensajeServerError((string)o.SelectToken("error_msg")); } } } else { //respuesta = e.SocketError.ToString(); msg = new MensajeServerError(e.SocketError.ToString()); } _clientDone_recive.Set(); } }); // Sets the state of the event to nonsignaled, causing threads to block _clientDone_recive.Reset(); // Make an asynchronous Receive request over the socket _socket.ReceiveAsync(socketEventArg); // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds. // If no response comes back within this time then proceed //_clientDone.WaitOne(TIMEOUT_MILLISECONDS); _clientDone_recive.WaitOne(); } else { //respuesta = "Socket is not initialized"; /*msg = new ChatMensaje("Socket is not initialized", * "room", * "SystemRecivFail", * 0);*/ msg = new MensajeServerError("Socket is not initialized"); } //return respuesta; return(msg); }