Esempio n. 1
0
        private async static void HandleSocket(SlackBotSocket botsocket, ClientWebSocket socket, ArraySegment <byte> buffer)
        {
            var i = 300;

            while (socket.State != WebSocketState.Open)
            {
                Thread.Sleep(10);
                if (--i <= 0)
                {
                    return;
                }
            }
            try
            {
                while (socket.State == WebSocketState.Open)
                {
                    var received = await socket.ReceiveAsync(buffer, CancellationToken.None);

                    if (received.MessageType == WebSocketMessageType.Close)
                    {
                        await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NormalClosure", CancellationToken.None);
                    }
                    else
                    {
                        var messageBytes = buffer.Skip(buffer.Offset).Take(received.Count).ToArray();

                        var rawMessage = new UTF8Encoding().GetString(messageBytes);
                        if (!string.IsNullOrWhiteSpace(rawMessage))
                        {
                            botsocket.OnDataReceived(JObject.Parse(rawMessage));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e is TaskCanceledException)
                {
                    return;
                }
                else
                {
                    throw e;
                }
            }
        }
Esempio n. 2
0
 public SocketDisposer(SlackBotSocket socket)
 {
     _socket = socket;
 }
Esempio n. 3
0
        //public SlackBot(string clientId, string clientSecret)
        //{
        //    api = new SlackBotApi(clientId, clientSecret);
        //    socket = new SlackBotSocket();
        //    socket.DataReceived += Socket_DataReceived;
        //}

        public SlackBot(string token)
        {
            api    = new SlackBotApi(token);
            socket = new SlackBotSocket();
            socket.DataReceived += Socket_DataReceived;
        }