简体   繁体   中英

Socket connection in swift

I am learning & developing ios application with swift.I have 2 tabs in my app,so I have 2 view controller.I need to connect socket server.But in which file ?

First tab is showing conversations list and second tab is chat interface.User is sending message from second tab.If someone sends a message to this user,I need to show this message in first tab.

I need to connect socket server but in which file ? I mean example: When message arrives to this user i need to save it to database and show user in second tab.Is view controller file good for this case ?

I would recommend checking out Alamofire . It is a fantastic networking library built entirely for Swift and has really picked up in popularity of the past few months. This makes it incredibly easy to call a web service to fetch data.

WebSockets

If you actually need to connect to your server using web sockets, then I'd check out SocketRocket built by the fine folks at Square. Here's a link to their project on Github .

Since you're new to iOS development, I'd suggest a simple architecture where you abstract the network calls out of your view controllers.

ChatManager

  • Internally manages all networking such as SocketRocket
  • Should be a singleton or property on UIApplicationDelegate
  • Has public API for sending message
  • Sends out notifications when it receives notifications back

class ChatManager {

    // Add property for socket

    class var sharedInstance: ChatManager {
        struct Singleton { static let instance = ChatManager() }
        return Singleton.instance
    }

    init() {
        // Create the socket
    }

    func sendMessage(message: String) {
        // Push the message onto the socket
    }

    // Delegate methods

    func messageReceived(message: String) {
        // Emit the message using NSNotificationCenter
    }
}

View Controller 1

  • Subscribes to notifications from ChatManager

class ViewController1 : UIViewController {
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Register for NSNotification coming from ChatManager
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}

View Controller 2

  • Subscribes to notifications from ChatManager
  • Sends new messages to the ChatManager to push through the socket

class ViewController2 : UIViewController {
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Register for NSNotification coming from ChatManager
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func userAddedNewChatMessage(message: String) {
        ChatManager.sharedInstance.sendMessage(message)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM