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

Support swift language #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 73 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,81 @@ Flutter Socket IO Plugin, supported Android + iOS (iOS installation guide is com

- 1. Copy the folder `example/ios/Runner/SocketObj` to `${PROJECT_ROOT}/ios/Runner/`

- 2. Replace the `example/ios/Runner/AppDelegate.m` line with `${PROJECT_ROOT}/ios/Runner/AppDelegate.m`.
- 2. [If your app's iOs development language is Objective-C] Replace the `example/ios/Runner/AppDelegate.m` line with `${PROJECT_ROOT}/ios/Runner/AppDelegate.m`.
(Notice: **You should merge the old one in your project to merge with the new from this plugin if you have some change on that file**)

- 3. Open `${PROJECT_ROOT}/ios/Podfile`, paste this line `pod 'Socket.IO-Client-Swift', '~> 13.3.0'` before the end of `target 'Runner' do` block

- 4. Run and Enjoy the plugin :)
- 3. [If your app's iOs development language is Swift] Add these lines in `${PROJECT_ROOT}/ios/Runner/AppDelegate.swift` AFTER `GeneratedPluginRegistrant.register(with: self)`
~~~
let socketChannel = FlutterMethodChannel(name: "flutter_socket_io", binaryMessenger: controller.binaryMessenger)
socketChannel.setMethodCallHandler { (call, result) in
guard let dict = call.arguments as? NSDictionary,
let socketNameSpace = dict[SOCKET_NAME_SPACE] as? String,
let socketDomain = dict[SOCKET_DOMAIN] as? String
else {
result(FlutterError(code: "ERROR", message: "Argument is wrong", details: nil));
return
}

switch call.method {
case SOCKET_INIT:
let query = dict[SOCKET_QUERY] as? String
let callback = dict[SOCKET_CALLBACK] as? String
var queryStringDictionary = [String: String]()
query?.components(separatedBy: "&")
.forEach { pair in
let components = pair.components(separatedBy: "=")
guard let key = components.first,
let value = components.last else {
return
}
queryStringDictionary[key] = value
}
SocketIOManager.shared()?.initSocket(socketChannel, domain: socketDomain, query: queryStringDictionary, namspace: socketNameSpace, callBackStatus: callback)
case SOCKET_CONNECT:
SocketIOManager.shared()?.connectSocket(socketDomain, namspace: socketNameSpace)
case SOCKET_DISCONNECT:
SocketIOManager.shared()?.disconnectDomain(socketDomain, namspace: socketNameSpace)
case SOCKET_SUBSCRIBES:
guard let data = (dict[SOCKET_DATA] as? String)?.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSMutableDictionary
else {
result(FlutterError(code: "ERROR", message: "Data is wrong", details: nil));
return
}
SocketIOManager.shared()?.subscribes(socketDomain, namspace: socketNameSpace, subscribes: json)
case SOCKET_UNSUBSCRIBES:
guard let data = (dict[SOCKET_DATA] as? String)?.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSMutableDictionary
else {
result(FlutterError(code: "ERROR", message: "Data is wrong", details: nil));
return
}
SocketIOManager.shared()?.unSubscribes(socketDomain, namspace: socketNameSpace, subscribes: json)
case SOCKET_UNSUBSCRIBES_ALL:
SocketIOManager.shared()?.unSubscribesAll(socketDomain, namspace: socketNameSpace)
case SOCKET_SEND_MESSAGE:
guard let event = dict[SOCKET_EVENT] as? String,
let message = dict[SOCKET_MESSAGE] as? String,
let callback = dict[SOCKET_CALLBACK] as? String
else {
result(FlutterError(code: "ERROR", message: "Event or message is wrong", details: nil));
return
}

SocketIOManager.shared()?.sendMessage(event, message: message, domain: socketDomain, namspace: socketNameSpace, callBackStatus: callback)
case SOCKET_DESTROY:
SocketIOManager.shared()?.destroySocketDomain(socketDomain, namspace: socketNameSpace)
case SOCKET_DESTROY_ALL:
SocketIOManager.shared()?.destroyAllSocket()
default:
result(FlutterMethodNotImplemented)
}
}
~~~

- 4. Open `${PROJECT_ROOT}/ios/Podfile`, paste this line `pod 'Socket.IO-Client-Swift', '~> 13.3.0'` before the end of `target 'Runner' do` block

- 5. Run and Enjoy the plugin :)


## Use the plugin
Expand Down