Example #1
0
    internal static int ProcessPublish(string path, string json, Session ses) {
      Topic cur=Topic.root;
      Type vt=null;

      string[] pt=path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
      int i=0;
      while(i<pt.Length && cur.Exist(pt[i])) {
        cur=cur.Get(pt[i++]);
      }
      if(string.IsNullOrEmpty(json) || json=="null") {                      // Remove
        if(i==pt.Length && MQTT.MqBroker.CheckAcl(ses==null?string.Empty:ses.userName, cur, TopicAcl.Delete)) {
          cur.Remove();
        }
        return 200;
      }
      if(i<pt.Length || cur.valueType==null) {                             // path not exist
        vt=X13.WOUM.ExConverter.Json2Type(json);
        if(!MQTT.MqBroker.CheckAcl(ses==null?string.Empty:ses.userName, cur, TopicAcl.Create)) {
          return 403;
        }
        cur=Topic.GetP(path, vt, ses==null?null:ses.owner);        // Create
      }

      if(cur.valueType!=null) {                 // Publish
        if(MQTT.MqBroker.CheckAcl(ses==null?string.Empty:ses.userName, cur, TopicAcl.Change)) {
          cur.FromJson(json, ses==null?null:ses.owner);
        }
      }
      return 200;
    }
Example #2
0
 protected override void OnOpen() {
   if(_wsMan==null) {
     _wsMan=Sessions;
   }
   string sid=null;
   if(Context.CookieCollection["sessionId"]!=null) {
     sid=Context.CookieCollection["sessionId"].Value;
   }
   System.Net.IPEndPoint remoteEndPoint = Context.UserEndPoint;
   {
     System.Net.IPAddress remIP;
     if(Context.Headers.Contains("X-Real-IP") && System.Net.IPAddress.TryParse(Context.Headers["X-Real-IP"], out remIP)) {
       remoteEndPoint=new System.Net.IPEndPoint(remIP, remoteEndPoint.Port);
     }
   }
   _ses=Session.Get(sid, remoteEndPoint);
   _subscriptions=new List<Topic.Subscription>();
   Send(string.Concat("I\t", _ses.id, "\t", (string.IsNullOrEmpty(_ses.userName)?(_disAnonym.value?"false":"null"):"true")));
   if(_verbose.value) {
     X13.Log.Debug("{0} connect webSocket", _ses.owner.name);
   }
 }
Example #3
0
 public static Session Get(string sid, System.Net.IPEndPoint ep, bool create=true) {
   Session s;
   if(string.IsNullOrEmpty(sid) || (s=sessions.Where(z => z.IsAlive).Select(z => z.Target as Session).FirstOrDefault(z => z!=null && z.id==sid && z.ip.Equals(ep.Address)))==null) {
     if(create) {
       s=new Session(ep);
       sessions.Add(new WeakReference(s));
     } else {
       s=null;
     }
   }
   return s;
 }
Example #4
0
 protected override void OnClose(CloseEventArgs e) {
   if(_ses!=null) {
     _ses.Close();
     if(_verbose.value) {
       X13.Log.Info("{0} Disconnect: [{1}]{2}", _ses.owner.name, e.Code, e.Reason);
     }
     _ses=null;
   }
   foreach(var s in _subscriptions) {
     Topic.root.Unsubscribe(s.path, s.func);
   }
 }