Ejemplo 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 static WebsocketSessionUpdate[] CreatePeerUpdates(WebsocketSessionUpdateStatus status, WebsocketSessionPeerToken peerToken)
        {
            WebsocketSessionPeerToken[] peerTokens = new WebsocketSessionPeerToken[]
            {
                peerToken
            };

            return(CreatePeerUpdates(status, peerTokens));
        }
        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);
        }
        private Task ProcessMessageRequest(WebsocketSessionMessageRequest request, string sessionKey, WebsocketSessionPeer peer)
        {
            // get a reference to the session
            WebsocketSession session = GetSessionByKey(sessionKey);

            // handle the different types of messages;
            string message = "";

            switch (request.Type)
            {
            case WebsocketSessionMessageType.Introduction:
                string hostId = session.GetAttributeValue <string>("hostId");
                WebsocketSessionPeerToken token = System.Text.Json.JsonSerializer.Deserialize <Models.WebsocketSessionPeerToken>(request.Message);
                peer.Token.DisplayName = token.DisplayName;
                peer.Token.IconUrl     = token.IconUrl;

                if (token.PeerId == hostId)
                {
                    // if host, no need to request access; just grant access;
                    WebsocketSessionUpdate[]        updates           = CreatePeerUpdates(WebsocketSessionUpdateStatus.AccessGranted, peer.Token);
                    WebsocketSessionMessageResponse hostAlertResponse = CreateWebsocketResponseMessage(WebsocketSessionMessageType.StatusUpdates, updates);
                    SendMessage(sessionKey, hostId, hostAlertResponse);
                    return(Task.CompletedTask);
                }
                message = request.Message;
                break;

            case WebsocketSessionMessageType.StatusUpdates:
            case WebsocketSessionMessageType.ByteArray:
            case WebsocketSessionMessageType.Text:
                message = request.Message;
                break;

            case WebsocketSessionMessageType.Reaction:
                if (request.TargetMessageId == null)
                {
                    throw new Exception("Reaction must provide TargetMessageId value.");
                }
                break;

            default:
                throw new Exception("Unknown Message Type: " + request.Type);
            }

            List <WebsocketSessionMessageResponse> messages = session.GetAttributeValue <List <WebsocketSessionMessageResponse> >("messages");

            WebsocketSessionMessageResponse messageResponse = new WebsocketSessionMessageResponse()
            {
                MessageId   = messages.Count,
                MessageType = request.Type,
                Message     = message,
                SenderId    = peer.Token.PeerId,
                Recipients  = request.Recipients
            };

            messages.Add(messageResponse);

            if (request.Recipients == null || request.Recipients.Length == 0)
            {
                SendMessageToPeers(sessionKey, peer.Token.PeerId, messageResponse);
            }
            else
            {
                SendMessage(sessionKey, request.Recipients, messageResponse);
            }
            return(Task.CompletedTask);
        }