Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code for swift #62

Open
ghost opened this issue Aug 14, 2021 · 1 comment
Open

code for swift #62

ghost opened this issue Aug 14, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 14, 2021

Hi,
You have provide objective-c cod in this path blow
example/ios/Runner/AppDelegate.m;
but where can I find cod example for swift?

@ward975
Copy link

ward975 commented Aug 28, 2021

let controller = window.rootViewController as? FlutterBinaryMessenger

methodChannel = FlutterMethodChannel(
    name: "flutter_socket_io",
    binaryMessenger: controller!)


methodChannel?.setMethodCallHandler({ (call, result) in
      
    if let JSON = call.arguments  as?  [String : Any] {
        
        let socketNameSpace = JSON[SOCKET_NAME_SPACE] as? String
        let socketDomain = JSON[SOCKET_DOMAIN] as? String
        let callback = JSON[SOCKET_CALLBACK] as? String
        
        if SOCKET_INIT == call.method {
            //NSString *query = @"userId=123123&abc=xxx";
            let query = JSON[SOCKET_QUERY] as? String
            var queryStringDictionary: [String : Any] = [:]
            let urlComponents = query?.components(separatedBy: "&")
            for keyValuePair in urlComponents ?? [] {
                let pairComponents = keyValuePair.components(separatedBy: "=")
                let key = pairComponents.first?.removingPercentEncoding
                let value = pairComponents.last?.removingPercentEncoding

                queryStringDictionary[key ?? ""] = value
            }
            SocketIOManager.shared().initSocket(methodChannel, domain: socketDomain, query: queryStringDictionary, namspace: socketNameSpace, callBackStatus: callback)
        } else if SOCKET_CONNECT == call.method {
            SocketIOManager.shared().connectSocket(socketDomain, namspace: socketNameSpace)
        } else if SOCKET_DISCONNECT == call.method {
            SocketIOManager.shared().disconnectDomain(socketDomain, namspace: socketNameSpace)
        } else if SOCKET_SUBSCRIBES == call.method {
            let socketData = JSON[SOCKET_DATA] as? String
            let data = socketData?.data(using: .utf8)
            var map: Any? = nil
            do {
                if let data = data {
                    map = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers]) as! NSMutableDictionary
                }
            } catch {
            }
            
            SocketIOManager.shared().subscribes(socketDomain, namspace: socketNameSpace, subscribes: map as? NSMutableDictionary)
        } else if SOCKET_UNSUBSCRIBES == call.method {
            let socketData = JSON[SOCKET_DATA] as? String
            let data = socketData?.data(using: .utf8)
            var map: Any? = nil
            do {
                if let data = data {
                    map = try JSONSerialization.jsonObject(with: data, options: [])
                }
            } catch {
            }
            SocketIOManager.shared().unSubscribes(socketDomain, namspace: socketNameSpace, subscribes: map as? NSMutableDictionary)
        }else if SOCKET_UNSUBSCRIBES_ALL == call.method {
            SocketIOManager.shared().unSubscribesAll(socketDomain, namspace: socketNameSpace)
        } else if SOCKET_SEND_MESSAGE == call.method {
            print("SOCKET_SEND_MESSAGE native")
            let event = JSON[SOCKET_EVENT] as? String
            let message = JSON[SOCKET_MESSAGE] as? String
            if event != nil && message != nil {
                SocketIOManager.shared().sendMessage(event, message: message, domain: socketDomain, namspace: socketNameSpace, callBackStatus: callback)
            }
        }else if SOCKET_DESTROY == call.method {
            SocketIOManager.shared()?.destroySocketDomain(socketDomain, namspace: socketNameSpace)
        }else if SOCKET_DESTROY_ALL == call.method {
            SocketIOManager.shared()?.destroyAllSocket()
        }else {
            result(FlutterMethodNotImplemented);
        }

    }
    
    
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant