public virtual void RemoveClient(object client) { try { if (client != null && client is Session) { Session session = client as Session; string requestPath = ""; var sessionData = WebMessage.GetSessionData(session, true); // make sure session data is ok if (sessionData.ContainsKey("Path")) { requestPath = sessionData["Path"].ToString(); } if (requestPath == null || requestPath.Length <= 0) { return; } List <string> paramList = new List <string>(); string[] parts = requestPath.Split('/'); foreach (var part in parts) { if (part.Trim().Length > 0) { paramList.Add(part.Trim()); } } if (paramList.Count <= 0) { return; } string sourceName = paramList.First(); if (sourceName != null && sourceName.Length > 0) { List <object> clients = null; lock (m_WebClients) { if (m_WebClients.ContainsKey(sourceName)) { clients = m_WebClients[sourceName]; } } if (clients != null) { lock (clients) { clients.Remove(session); } } } } } catch (Exception ex) { Logger.Error("Failed to remove client: " + ex.Message); } }
public string GetRequestPath() { var url = ""; if (m_Session != null) { url = WebMessage.GetSessionData(m_Session, "Path").ToString(); } return(url); }
//public override int OnSend(Session session, object data) //{ // int result = base.OnSend(session, data); // m_WebSocketServer.Logger.Info("On Send - " + data.ToString()); // return result; //} protected override void ProcessMessage(SessionContext ctx) { if (ctx == null) { return; } WebMessage msg = ctx.Data as WebMessage; if (msg == null || !msg.IsString()) { ctx.Session.Close(); return; } else { var reqctx = new RequestContext(new WebSocketSession(ctx.Session), msg.MessageContent, m_WebSocketServer.Flags); reqctx.RequestPath = WebMessage.GetSessionData(ctx.Session, "Path").ToString(); reqctx.Headers = new Dictionary <string, string>(msg.Headers); m_WebSocketServer.RequestHandler.HandleRequest(reqctx); } }
public virtual void ValidateClient(object client) { Session session = client as Session; if (session == null) { return; } Task.Factory.StartNew(() => { try { string target = ""; string reqpath = WebMessage.GetSessionData(session, "Path").ToString(); if (m_ClientValidator != null) { target = m_ClientValidator.Validate(session.GetRemoteIp(), reqpath); } else { List <string> paramList = new List <string>(); string[] parts = reqpath.Split('/'); foreach (var part in parts) { if (part.Trim().Length > 0) { paramList.Add(part.Trim()); } } if (paramList.Count > 0) { target = paramList.First(); } } if (target != null && target.Length > 0) { bool isOK = true; try { byte[] welcomeData = null; string welcomeText = ""; var channel = m_ResourceManager.GetChannel(target, false); if (channel != null) { welcomeData = channel.GetWelcomeData(); welcomeText = channel.GetWelcomeText(); } if (welcomeData != null && welcomeData.Length > 0) { session.Send(new WebMessage(welcomeData)); } if (welcomeText != null && welcomeText.Length > 0) { session.Send(new WebMessage(welcomeText)); } } catch { isOK = false; } if (isOK) { List <object> clients = null; int channelClientCount = 0; lock (m_WebClients) { if (m_WebClients.ContainsKey(target)) { clients = m_WebClients[target]; } } if (clients != null) { lock (clients) { clients.Add(session); channelClientCount = clients.Count; } } if (m_InputPort <= 0) { // update state lock (m_States) { if (!m_States.ContainsKey(target)) { var channelState = new MediaChannelState(); channelState.ChannelName = target; channelState.ClientCount = channelClientCount; channelState.ServerInfo = m_ServerName + "(" + m_InputIp + ":" + m_InputPort + "/" + m_OutputIp + ":" + m_OutputPort + ")"; m_States.Add(target, channelState); } } } } } else { session.Close(); // just close it if failed to validate } } catch (Exception ex) { Logger.Error("Validation error: " + ex.Message); } }); }