public bool sendMessage(MensajeServerMensaje m) { var ret = client.SendMessage(m); if (ret == "Success") { salasConectado.Add(m.room); return(true); } return(false); }
public string SendMessage(MensajeServerMensaje data) { string response = "Operation Timeout SendMessage"; char comunication = 's'; // We are re-using the _socket object initialized in the Connect method if (_socket != null) { // Create SocketAsyncEventArgs context object SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); // Set properties on context object socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint; socketEventArg.UserToken = null; // Inline event handler for the Completed event. // Note: This event handler was implemented inline in order // to make this method self-contained. socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { if (comunication == 's') { response = e.SocketError.ToString(); // Unblock the UI thread _clientDone_send.Set(); } }); //Transformo el ChatMessage a JSON string dataJSON = JsonConvert.SerializeObject(data); // Add the data to be sent into the buffer byte[] payload = Encoding.UTF8.GetBytes(dataJSON); socketEventArg.SetBuffer(payload, 0, payload.Length); // Sets the state of the event to nonsignaled, causing threads to block _clientDone_send.Reset(); // Make an asynchronous Send request over the socket _socket.SendAsync(socketEventArg); // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds. // If no response comes back within this time then proceed _clientDone_send.WaitOne(TIMEOUT_MILLISECONDS); } else { response = "Socket is not initialized"; } return(response); }
private void Mandar_Click(object sender, RoutedEventArgs e) { if (Mensaje.Text != "") { //bw.CancelAsync(); MensajeServerMensaje mandar = new MensajeServerMensaje(Mensaje.Text, sala, AplicationSettings.getUsuario(), Convert.ToInt32(AplicationSettings.getIdUsuario())); hilo.sendMessage(mandar); //ponerMensaje("<Tú>" + Mensaje.Text, true); cargarMensajes(); ponerMensaje(mandar, true); Mensaje.Text = ""; //if (!bw.IsBusy) //bw.RunWorkerAsync(); } }
/// <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); }
private void ponerMensaje(MensajeServerMensaje mensaje, bool me) { Grid panelTexto = new Grid(); //panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal; Thickness marginpanel = panelTexto.Margin; marginpanel.Bottom = 10; panelTexto.Margin = marginpanel; //Creo los colores SolidColorBrush yellowBrush = new SolidColorBrush(); yellowBrush.Color = Colors.Yellow; SolidColorBrush blackBrush = new SolidColorBrush(); blackBrush.Color = Colors.Black; //Creo el triangulo Polygon yellowTriangle = new Polygon(); yellowTriangle.Fill = yellowBrush; //Creo sus puntos System.Windows.Point Point1 = new System.Windows.Point(0, 0); System.Windows.Point Point2 = new System.Windows.Point(10, 0); System.Windows.Point Point3; if (!me) { Point3 = new System.Windows.Point(10, 10); } else { Point3 = new System.Windows.Point(0, 10); } PointCollection polygonPoints = new PointCollection(); polygonPoints.Add(Point1); polygonPoints.Add(Point2); polygonPoints.Add(Point3); //Añado los puntos al triangulo yellowTriangle.Points = polygonPoints; //Creo el mensaje con el texto Grid gridParaTexto = new Grid();// En WP los textBlock notienecolor de fondo, usamos un gridpara ponerle ese color de fondo gridParaTexto.Background = yellowBrush; //Mejoramos la interfaz del mensaje gridParaTexto.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, }); gridParaTexto.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, }); TextBlock name = new TextBlock(); name.Text = mensaje.name + ":"; name.Foreground = blackBrush; gridParaTexto.Children.Add(name); TextBlock texto = new TextBlock(); texto.TextWrapping = TextWrapping.Wrap; texto.Text = mensaje.message; texto.Foreground = blackBrush; gridParaTexto.Children.Add(texto); Grid.SetRow(name, 0); Grid.SetRow(texto, 1); panelTexto.Children.Add(yellowTriangle); panelTexto.Children.Add(gridParaTexto); //Añado el texto segun quien pusiera el mensaje if (!me) { //panelTexto.Children.Add(yellowTriangle); //panelTexto.Children.Add(gridParaTexto); panelTexto.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, }); panelTexto.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star), }); Grid.SetColumn(gridParaTexto, 1); Grid.SetColumn(yellowTriangle, 0); chat.Children.Add(panelTexto); } else { //panelTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; //panelTexto.Children.Add(gridParaTexto); //panelTexto.Children.Add(yellowTriangle); panelTexto.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star), }); panelTexto.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, }); gridParaTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; Grid.SetColumn(gridParaTexto, 0); Grid.SetColumn(yellowTriangle, 1); chat.Children.Add(panelTexto); } //Estas 2 lineas de abajo me bajan el ScrollView cuando esta lleno de mensajes. ScrollViewer.UpdateLayout(); ScrollViewer.ScrollToVerticalOffset(double.MaxValue); }
public void insert(MensajeServerMensaje uni) { dbConn.InsertOrReplace(uni); }