Example #1
0
        // remove the peer matching an ID or address string: user|id
        public override JObject remove(string id)
        {
            var addr = Symple.parseAddress(id);

            id = (string)addr["id"] ?? id;
            var peer = base.remove(id);

            if (peer != null)
            {
                this.client.dispatch("removePeer", peer);
            }
            return(peer);
        }
Example #2
0
        // get the peer matching an ID or address string: user|id
        public override JObject get(string id)
        {
            // handle IDs
            JObject peer = base.get(id);

            if (peer != null)
            {
                return(peer);
            }

            // handle address strings
            return(this.findOne(Symple.parseAddress(id)));
        }
Example #3
0
        public void connect()
        {
#if NETFX_CORE
            Messenger.Broadcast(SympleLog.LogInfo, "symple:client: connecting");

            if (this.socket != null)
            {
                string err = "the client socket is not null";
                Messenger.Broadcast(SympleLog.LogError, err);
                throw new Exception(err);
            }

            this.socket = IO.Socket(this.options["url"].ToString(), this.ioOptions);
            this.socket.On(Socket.EVENT_CONNECT, () => {
                Messenger.Broadcast(SympleLog.LogInfo, "symple:client: connected");
                Messenger.Broadcast(SympleLog.Connected);

                JObject announceData  = new JObject();
                announceData["user"]  = this.peer["user"] ?? "";
                announceData["name"]  = this.peer["name"] ?? "";
                announceData["type"]  = this.peer["type"] ?? "";
                announceData["token"] = options["token"] ?? "";

                string announceDataJsonString = JsonConvert.SerializeObject(announceData, Formatting.None);
                Messenger.Broadcast(SympleLog.LogTrace, "announceDataJsonString: " + announceDataJsonString);

                this.socket.Emit("announce", (resObj) => {
                    Messenger.Broadcast(SympleLog.LogDebug, "symple:client: announced " + resObj);
                    Messenger.Broadcast(SympleLog.Announced);

                    JObject res = (JObject)resObj;

                    if ((int)res["status"] != 200)
                    {
                        this.setError("auth", resObj.ToString());
                        return;
                    }

                    JObject resData = (JObject)res["data"];

                    this.peer = Symple.extend(this.peer, resData);
                    this.roster.add(resData);

                    JObject sendPresenceParams  = new JObject();
                    sendPresenceParams["probe"] = true;

                    this.sendPresence(sendPresenceParams);
                    this.dispatch("announce", res);
                    this.socket.On(Socket.EVENT_MESSAGE, (msg) =>
                    {
                        Messenger.Broadcast(SympleLog.LogTrace, "symple:client receive " + msg);

                        JObject m = (JObject)msg;

                        string mType = (string)m["type"];

                        switch (mType)
                        {
                        case "message":
                            m = new SympleMessage(m);
                            break;

                        case "command":
                            m = new SympleCommand(m);
                            break;

                        case "event":
                            m = new SympleEvent(m);
                            break;

                        case "presence":
                            m = new SymplePresence(m);
                            if ((bool)m["data"]["online"])
                            {
                                this.roster.update((JObject)m["data"]);
                            }
                            else
                            {
                                this.roster.remove((string)m["data"]["id"]);
                            }

                            if (m["probe"] != null && (bool)m["probe"] == true)
                            {
                                JObject presenceTo = new JObject();
                                presenceTo["to"]   = Symple.parseAddress(m["from"].ToString())["id"];

                                this.sendPresence(new SymplePresence(presenceTo));
                            }
                            break;

                        default:
                            var o     = m;
                            o["type"] = o["type"] ?? "message";
                            break;
                        }

                        if (m["from"].Type != JTokenType.String)
                        {
                            Messenger.Broadcast(SympleLog.LogError, "symple:client: invalid sender address: " + m);
                            return;
                        }

                        // replace the from attribute with the full peer object.
                        // this will only work for peer messages, not server messages.

                        string mFrom = (string)m["from"];
                        Messenger.Broadcast(SympleLog.LogTrace, "looking up rpeer in roster, mFrom = " + mFrom + "...");

                        var rpeer = this.roster.get(mFrom);
                        if (rpeer != null)
                        {
                            Messenger.Broadcast(SympleLog.LogTrace, "found rpeer: " + rpeer);
                            m["from"] = rpeer;
                        }
                        else
                        {
                            Messenger.Broadcast(SympleLog.LogInfo, "symple:client: got message from unknown peer: " + m);
                        }

                        // Dispatch to the application
                        this.dispatch((string)m["type"], m);
                    });
                }, announceData);
            });

            this.socket.On(Socket.EVENT_ERROR, () =>
            {
                // this is triggered when any transport fails, so not necessarily fatal
                this.dispatch("connect");
            });

            this.socket.On("connecting", () =>
            {
                Messenger.Broadcast(SympleLog.LogDebug, "symple:client: connecting");
                this.dispatch("connecting");
            });

            this.socket.On(Socket.EVENT_RECONNECTING, () =>
            {
                Messenger.Broadcast(SympleLog.LogDebug, "symple:client: connecting");
                Messenger.Broadcast(SympleLog.Reconnecting);
                this.dispatch("reconnecting");
            });

            this.socket.On("connect_failed", () =>
            {
                // called when all transports fail
                Messenger.Broadcast(SympleLog.LogError, "symple:client: connect failed");
                Messenger.Broadcast(SympleLog.ConnectFailed);
                this.dispatch("connect_failed");
                this.setError("connect");
            });

            this.socket.On(Socket.EVENT_DISCONNECT, () =>
            {
                Messenger.Broadcast(SympleLog.LogInfo, "symple:client: disconnect");
                Messenger.Broadcast(SympleLog.Disconnect);
                this.peer["online"] = false;
                this.dispatch("disconnect");
            });
#endif
        }