Ejemplo n.º 1
0
        public void SendCallback(IAsyncResult ar)
        {
            // TODO: Change tstaus to not sent
            SendStateObject so            = (SendStateObject)ar.AsyncState;
            MessageModel    messageModel  = so.extra as MessageModel;
            string          StatusMessage = "";

            if (messageModel == null)
            {
                return;
            }
            if (!so.WorkSocket.Connected)
            {
                StatusMessage = "Not delivered";
            }
            else
            {
                int bytesSent = so.WorkSocket.EndSend(ar);
                if (bytesSent == so.MessageLength)
                {
                    StatusMessage = "Delivered";
                }
                else
                {
                    StatusMessage = "Not delivered";
                }
            }

            Application.Current.Dispatcher.Invoke(() => messageModel.StatusMessage = StatusMessage);
        }
Ejemplo n.º 2
0
        public void Send(MessageModel Message)
        {
            SendStateObject state = new SendStateObject();

            state.WorkSocket = _ConSocket;
            state.extra      = Message;

            MessageBase sendMessage = null;

            if (Message is TextMessageModel)
            {
                sendMessage = new TextChatMessage((TextMessageModel)Message, Model.Username, Model.id);
            }
            else if (Message is ImageMessageModel)
            {
                sendMessage = new ImageChatMessage((ImageMessageModel)Message, Model.Username, Model.id);
            }
            else if (Message is BuzzMessageModel)
            {
                sendMessage = new BuzzMessage(Model.Username, Model.id);
            }
            string serializedMessage = sendMessage.Serialize();

            byte[] byteMessage = Encoding.UTF8.GetBytes(serializedMessage);
            state.MessageLength = byteMessage.Length;
            try
            {
                state.WorkSocket.BeginSend(byteMessage, 0, state.MessageLength, SocketFlags.None, SendCallback, state);
            }
            catch (Exception ex) when(ex is SocketException || ex is NullReferenceException)
            {
                throw new NoConnectionException();
            }
        }