socket.on回调在socket.io中重复多次

时间:2019-03-13 11:50:44

标签: swift xcode sockets socket.io

我正在使用pod 'Socket.IO-Client-Swift', '~> 14.0.0' 这是我的套接字管理器文件 我不知道为什么当我一次发送消息一次调用此消息一次时,socketGetLatestMsg()回调重复多次,但是在第二次消息回调之后,在3 msg回调重复三次之后重复两次,依此类推... 然后返回到用户列表收件箱,并返回给同一用户,所有重复的消息均按原样显示,但在数据库中不保存重复的消息。

class SocketIOManager: NSObject {

    static let shared = SocketIOManager(chatID: nil, token: nil);
    var socket: SocketIOClient?
    var manager: SocketManager?


    init(chatID: Int?, token: String?) {
        super.init();

        if chatID != nil && token != nil {
            manager = SocketManager(socketURL: URL(string: SOCKET_URL)!, config: [.log(true), .compress, .connectParams(["auth_token": token!, "user_id": chatID!])])

            self.socket = manager!.defaultSocket;

        } else if UserDefaults.standard.value(forKey: "userID") != nil && UserDefaults.standard.value(forKey: "authToken") != nil {

            manager = SocketManager(socketURL: URL(string: SOCKET_URL)!, config: [.log(true), .compress, .connectParams(["auth_token": UserDefaults.standard.value(forKey: "authToken") as! String, "user_id": UserDefaults.standard.value(forKey: "userID") as! Int])])
            self.socket = manager!.defaultSocket;
        }

        self.socket?.connect();


    }


    //MARK:- All Emit
    func socketInboxEmit(userID: Int?,dictData: [String: Any]?) {
        if dictData != nil {

            self.socket?.on(clientEvent: .connect, callback: { (data, ack) in
                self.socket?.emit("chatroom.inbox", dictData!);

                self.getInboxList(chatID: userID);
                print("get Inbox socket List");
            })

        }
    }

    func socketGetUsrMsgEmit(chatListID: Int?, userID: Int?, dictData: [String: Any]?) {
        if chatListID != nil && dictData != nil && userID != nil {
            self.socket?.emit("chatroom.messages", dictData!);
            self.socketGetAllMsg(userID: userID, chatListID: chatListID!);
        }
    }

    func socketSaveMsg(chatListID: Int, dictData: [String: Any]?) {

        //self.socket?.on(clientEvent: .connect, callback: { (data, ack) in
            self.socket?.emit("save_message", dictData!);
            self.socketGetLatestMsg(chatListID: chatListID);
            print("socket connected");
        //})

    }



    func contactListEmit(dictData: [String: Any]?) {
        self.socket?.emit("chatroom.friends", dictData!);
    }


    //MARK:- Get All Data
    func socketCallBack(chatID: Int?, token: String?, completionHandler: @escaping (String, [String: Any]?) -> Void) {
        if chatID != nil {
            completionHandler("Fail", nil)
        }
        else {
            print("chat id not match or nil");
        }

    }

    func socketGetLatestMsg(chatListID: Int?) {
        socket?.on("chatroom.\(chatListID!)", callback: { (data, ack) in

            for filterData in data {
                if ((filterData as? [String: Any]) != nil) {
                    ChatVM.sharedInstance.appendNewMsg(responseDict: filterData as! JSONDictionary)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tblReload"), object: nil, userInfo: ["table": "reload"])
                } else {
                    print("Ok")
                }
            }
        })
    }

    func socketGetAllMsg(userID: Int?, chatListID: Int) {
        self.socket?.on("chat.\(userID!).room.\(chatListID)", callback: { (data, ack) in
            for filterData in data {
                if ((filterData as? [String: Any]) != nil) {
                    ChatVM.sharedInstance.parseChatListData(responseDict: filterData as! JSONDictionary)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tblReload"), object: nil, userInfo: ["table": "reload"])
                } else {
                    print("Ok")
                }
            }


        })
    }

    func getInboxList(chatID: Int?) {

        socket?.on("user.\(chatID!).chatinbox", callback: { (data, ack) in

            for filterData in data {
                if ((filterData as? [String: Any]) != nil) {


                    ChatVM.sharedInstance.parseInboxListData(responseDict: filterData as! JSONDictionary);

                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tblInboxReload"), object: nil);
                    //completionHandler("Success", filterData as? [String : Any])
                } else {
                    print("Ok")
                    //completionHandler("Fail", nil)
                }
            }
        })

    }


    func getContactList(userID: Int?) {
        socket?.on("user.\(userID!).friends", callback: { (data, ack) in

            for filterData in data {
                if ((filterData as? [String: Any]) != nil) {

                    ChatVM.sharedInstance.parseFriendListData(responseDict: filterData as! JSONDictionary)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tblPendingReload"), object: nil, userInfo: filterData as! [AnyHashable : Any])
                    //completionHandler("Success", filterData as? [String : Any])
                } else {
                    print("Ok")
                    //completionHandler("Fail", nil)
                }
            }
        })
    }

    func closeSocket() {
        self.socket?.disconnect();
    }

    func activeSocket() {
        self.socket?.connect();
    }
}

0 个答案:

没有答案