Пример #1
0
        private async Task Send(SlackResponse slackResponse)
        {
            WriteLineToConsole($"Sending message");

            string json = Serialiser.ToJsonString <SlackResponse>(slackResponse);

            byte[] buffer = Encoding.UTF8.GetBytes(json);

            await _clientWebSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
        }
Пример #2
0
        private async Task Receive()
        {
            while (_clientWebSocket.State == WebSocketState.Open)
            {
                byte[] buffer = new byte[1024];
                WebSocketReceiveResult webSocketReceiveResult = await _clientWebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

                if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close)
                {
                    await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                }
                else
                {
                    string message     = Encoding.UTF8.GetString(buffer).TrimEnd('\0');
                    string messageType = "";

                    if (message.Contains("\"type\":\"") == true)
                    {
                        messageType = message.Substring(message.IndexOf("\"type\":\"") + "\"type\":\"".Length);

                        if (messageType.Contains("\"") == true)
                        {
                            messageType = messageType.Substring(0, messageType.IndexOf("\""));
                        }
                    }

                    if (String.IsNullOrWhiteSpace(messageType) == false)
                    {
                        SlackMessage slackMessage = null;

                        switch (messageType.ToLower())
                        {
                        case "desktop_notification":
                            slackMessage = Serialiser.FromString <DesktopNotificationSlackMessage>(message);
                            break;

                        case "hello":
                            slackMessage = Serialiser.FromString <HelloSlackMessage>(message);
                            break;

                        case "message":
                            MessageSlackMessage oMessageSlackMessage = Serialiser.FromString <MessageSlackMessage>(message);

                            OnMessageReceived(oMessageSlackMessage);

                            slackMessage = oMessageSlackMessage;
                            break;

                        case "presence_change":
                            slackMessage = Serialiser.FromString <PresenceChangeSlackMessage>(message);
                            break;

                        case "reconnect_url":
                            slackMessage = Serialiser.FromString <ReconnectUrlSlackMessage>(message);
                            break;

                        case "user_typing":
                            slackMessage = Serialiser.FromString <UserTypingSlackMessage>(message);
                            break;

                        default:
                            WriteLineToConsole($"Unhandled message type: {messageType}");
                            break;
                        }
                    }
                }
            }
        }