Exemplo n.º 1
0
        public async override Task <WebsocketSessionPeer> JoinSession(HttpContext context, string sessionType, string sessionKey)
        {
            WebsocketSession targetSession = GetSessionByKey(sessionKey);

            if (targetSession == null)
            {
                targetSession = this.CreateSession(sessionType, sessionKey);
            }

            if (targetSession.GetPeers().Count > 0)
            {
                throw new Exception("Unable to join Progress session that already has a subscriber.");
            }

            WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();

            WebsocketSessionPeer peer = targetSession.AddPeer(socket);

            targetSession.SetAttribute("hostId", peer.Token.PeerId);

            // alert requester of their own token
            WebsocketSessionPeerToken hostToken = peer.Token;
            WebsocketSessionGreeting  greeting  = new WebsocketSessionGreeting {
                SessionKey = sessionKey, HostToken = hostToken, Token = peer.Token
            };
            WebsocketSessionMessageResponse greetingResponse = CreateWebsocketResponseMessage(WebsocketSessionMessageType.Greeting, greeting);

            SendMessage(peer, greetingResponse);

            return(peer);
        }
        public virtual async Task <WebsocketSessionPeer> JoinSession(HttpContext context, string sessionType, string sessionKey)
        {
            WebsocketSession targetSession = GetSessionByKey(sessionKey);

            if (targetSession == null)
            {
                targetSession = this.CreateSession(sessionType, sessionKey);
            }

            WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();

            WebsocketSessionPeer sessionSocket = targetSession.AddPeer(socket);

            string hostId = sessionSocket.Token.PeerId;

            if (targetSession.GetPeers().Count == 1)
            {
                targetSession.SetAttribute("hostId", hostId);
            }
            else
            {
                hostId = targetSession.GetAttributeValue <string>("hostId");
            }

            // alert requester of their own token, the hosts token and other existing peers
            WebsocketSessionPeerToken hostToken = targetSession.GetPeerById(hostId).Token;

            WebsocketSessionPeerToken[] participants = targetSession.GetPeers().Where(pair => pair.Value.Token.PeerId != sessionSocket.Token.PeerId).Select(pair => pair.Value.Token).ToArray();

            WebsocketSessionGreeting greeting = new WebsocketSessionGreeting {
                SessionKey = sessionKey, HostToken = hostToken, Token = sessionSocket.Token, Peers = participants
            };
            WebsocketSessionMessageResponse greetingResponse = CreateWebsocketResponseMessage(WebsocketSessionMessageType.Greeting, greeting);

            SendMessage(sessionSocket, greetingResponse);

            return(sessionSocket);
        }
        public virtual async Task SendBinaryToPeers(string sessionKey, WebSocket socket, ArraySegment <byte> buffer)
        {
            WebsocketSession session = this.GetSessionByKey(sessionKey);

            if (session == null)
            {
                throw new Exception("Unknown Session requested: " + sessionKey);
            }

            foreach (var pair in session.GetPeers())
            {
                if (pair.Value.Socket != socket && pair.Value.Socket.State == WebSocketState.Open)
                {
                    await SendBinary(pair.Value.Socket, buffer);
                }
            }
        }
        public virtual async Task SendMessageToPeers(string sessionKey, WebSocket socket, string message)
        {
            WebsocketSession session = this.GetSessionByKey(sessionKey);

            if (session == null)
            {
                throw new Exception("Unknown Session requested: " + sessionKey);
            }

            foreach (var pair in session.GetPeers())
            {
                if (pair.Value.Socket != socket && pair.Value.Socket.State == WebSocketState.Open)
                {
                    await SendMessage(pair.Value.Socket, message);
                }
            }
        }
        public async Task SendMessage(string sessionKey, IEnumerable <string> socketIds, string message)
        {
            WebsocketSession session = this.GetSessionByKey(sessionKey);

            if (session == null)
            {
                throw new Exception("Unknown Session requested: " + sessionKey);
            }

            foreach (var pair in session.GetPeers())
            {
                if (socketIds.Contains(pair.Key) && pair.Value.Socket.State == WebSocketState.Open)
                {
                    await SendMessage(pair.Value.Socket, message);
                }
            }
        }
        public async Task SendMessage(string sessionKey, IEnumerable <WebSocket> sockets, string message)
        {
            List <Task>      tasks   = new List <Task>();
            WebsocketSession session = this.GetSessionByKey(sessionKey);

            if (session == null)
            {
                throw new Exception("Unknown Session requested: " + sessionKey);
            }

            foreach (var pair in session.GetPeers())
            {
                if (sockets.Contains(pair.Value.Socket) && pair.Value.Socket.State == WebSocketState.Open)
                {
                    tasks.Add(SendMessage(pair.Value.Socket, message));
                }
            }
            await Task.WhenAll(tasks);
        }