简体   繁体   English

快速插座连接

[英]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 ? 我正在用swift学习和开发ios应用程序。我的应用程序中有2个选项卡,所以我有2个视图控制器。我需要连接套接字服务器。但是在哪个文件中?

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 . 我建议您检查Alamofire It is a fantastic networking library built entirely for Swift and has really picked up in popularity of the past few months. 这是一个完全为Swift构建的出色的网络库,并且在过去的几个月中确实得到了普及。 This makes it incredibly easy to call a web service to fetch data. 这使得调用Web服务来获取数据变得异常容易。

WebSockets Web套接字

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. 如果您实际上需要使用Web套接字连接到服务器,则可以查看Square的优秀人员构建的SocketRocket Here's a link to their project on Github . 这是他们在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. 由于您是iOS开发的新手,因此建议您使用一种简单的体系结构,在该体系结构中从视图控制器中提取网络调用。

ChatManager 聊天管理器

  • Internally manages all networking such as SocketRocket 内部管理所有网络,例如SocketRocket
  • Should be a singleton or property on UIApplicationDelegate 应该是UIApplicationDelegate上的单例或属性
  • Has public API for sending message 具有用于发送消息的公共API
  • 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 视图控制器1

  • Subscribes to notifications from ChatManager 订阅来自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 视图控制器2

  • Subscribes to notifications from ChatManager 订阅来自ChatManager的通知
  • Sends new messages to the ChatManager to push through the socket 向ChatManager发送新消息以推送套接字

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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