public void ProcessBody(Body body, HttpListenerContext ctx) { log.DebugFormat("Start process body connection {0}", Id); lock (locker) { CloseRequest(false); lastRequestTime = DateTime.Now; currentRequest = new BoshXmppRequest(Id, body, ctx); } if (body.HasChildElements) { if (body.FirstChild.GetAttribute("type") == "notification") { //сonfiguring store and write to data base DbPushStore dbPushStore = new DbPushStore(); var properties = new Dictionary <string, string>(1); properties.Add("connectionStringName", "default"); dbPushStore.Configure(properties); dbPushStore.SaveUserEndpoint( body.FirstChild.GetAttribute("username"), body.FirstChild.GetAttribute("endpoint"), body.FirstChild.GetAttribute("browser")); } } if (body.Type == BoshType.terminate) { Close(); return; } if (Volatile.Read(ref closed) == 1) { CloseRequest(true); return; } IdleWatcher.UpdateTimeout(Id, waitTimeout); if (string.IsNullOrEmpty(body.Sid) || body.XmppRestart) { var stream = new Stream { Prefix = Uri.PREFIX, Namespace = Uri.STREAM, Version = body.Version, Language = body.GetAttribute("lang"), To = body.To, }; XmppStreamStart(this, new XmppStreamStartEventArgs(Id, stream, Uri.CLIENT)); } foreach (var element in body.ChildNodes.OfType <Element>()) { XmppStreamElement(this, new XmppStreamEventArgs(Id, element)); } Send((Node)null, null); // try to send a non-empty buffer }
public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context) { if (!message.HasTo || message.To.IsServer) { context.Sender.SendTo(stream, XmppStanzaError.ToServiceUnavailable(message)); return; } var sessions = context.SessionManager.GetBareJidSessions(message.To); if (0 < sessions.Count) { foreach (var s in sessions) { try { context.Sender.SendTo(s, message); } catch { context.Sender.SendToAndClose(s.Stream, message); } } } else { pushStore = new DbPushStore(); var properties = new Dictionary <string, string>(1); properties.Add("connectionStringName", "default"); pushStore.Configure(properties); if (message.HasTag("active")) { var fromFullName = message.HasAttribute("username") ? message.GetAttribute("username") : message.From.ToString(); var messageToDomain = message.To.ToString().Split(new char[] { '@' })[1]; var tenant = CoreContext.TenantManager.GetTenant(messageToDomain); var tenantId = tenant != null ? tenant.TenantId : -1; var userPushList = new List <UserPushInfo>(); userPushList = pushStore.GetUserEndpoint(message.To.ToString().Split(new char[] { '@' })[0]); var firebaseAuthorization = ""; try { CallContext.SetData(TenantManager.CURRENT_TENANT, new Tenant(tenantId, "")); firebaseAuthorization = FireBase.Instance.Authorization; } catch (Exception exp) { log.DebugFormat("firebaseAuthorizationERROR: {0}", exp); } foreach (var user in userPushList) { try { var from = message.From.ToString().Split(new char[] { '@' })[0]; List <string> userId; string photoPath = ""; using (var db = new DbManager("core")) using (var command = db.Connection.CreateCommand()) { var q = new SqlQuery("core_user").Select("id").Where(Exp.Like("username", from)).Where("tenant", tenantId); userId = command.ExecuteList(q, DbRegistry.GetSqlDialect(db.DatabaseId)) .ConvertAll(r => Convert.ToString(r[0])) .ToList(); } if (userId.Count != 0) { var guid = new Guid(userId[0]); photoPath = UserPhotoManager.GetBigPhotoURL(guid); } var tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var data = new { to = user.endpoint, data = new { msg = message.Body, fromFullName = fromFullName, photoPath = photoPath } }; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(data); var byteArray = Encoding.UTF8.GetBytes(json); tRequest.Headers.Add(string.Format("Authorization: key={0}", firebaseAuthorization)); tRequest.ContentLength = byteArray.Length; using (var dataStream = tRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); using (var tResponse = tRequest.GetResponse()) { using (var dataStreamResponse = tResponse.GetResponseStream()) { using (var tReader = new StreamReader(dataStreamResponse)) { var sResponseFromServer = tReader.ReadToEnd(); var str = sResponseFromServer; } } } } } catch (Exception ex) { var str = ex.Message; log.DebugFormat("PushRequestERROR: {0}", str); } } } StoreOffline(message, context.StorageManager.OfflineStorage); } }
public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context) { if (!message.HasTo || message.To.IsServer) { context.Sender.SendTo(stream, XmppStanzaError.ToServiceUnavailable(message)); return; } var sessions = context.SessionManager.GetBareJidSessions(message.To); if (0 < sessions.Count) { foreach (var s in sessions) { try { context.Sender.SendTo(s, message); } catch { context.Sender.SendToAndClose(s.Stream, message); } } } else { pushStore = new DbPushStore(); var properties = new Dictionary <string, string>(1); properties.Add("connectionStringName", "default"); pushStore.Configure(properties); if (message.HasTag("active")) { var fromFullName = message.HasAttribute("username") ? message.GetAttribute("username") : message.From.ToString(); var tenantId = message.HasAttribute("tenantid") ? Convert.ToInt32(message.GetAttribute("tenantid"), 16) : -1; var userPushList = new List <UserPushInfo>(); userPushList = pushStore.GetUserEndpoint(message.To.ToString().Split(new char[] { '@' })[0]); var firebaseAuthorization = ""; try { CallContext.SetData(TenantManager.CURRENT_TENANT, new Tenant(tenantId, "")); firebaseAuthorization = KeyStorage.Get("firebase_authorization"); } catch (Exception exp) { log.DebugFormat("firebaseAuthorizationERROR: {0}", exp); } foreach (var user in userPushList) { try{ var tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var data = new { to = user.endpoint, data = new { msg = message.Body, fromFullName = fromFullName } }; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(data); var byteArray = Encoding.UTF8.GetBytes(json); tRequest.Headers.Add(string.Format("Authorization: key={0}", firebaseAuthorization)); tRequest.ContentLength = byteArray.Length; using (var dataStream = tRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); using (var tResponse = tRequest.GetResponse()) { using (var dataStreamResponse = tResponse.GetResponseStream()) { using (var tReader = new StreamReader(dataStreamResponse)) { var sResponseFromServer = tReader.ReadToEnd(); var str = sResponseFromServer; } } } } } catch (Exception ex) { var str = ex.Message; log.DebugFormat("PushRequestERROR: {0}", str); } } } StoreOffline(message, context.StorageManager.OfflineStorage); } }