[英]Swift 3 Singleton Manager Socket Connection
Is this the proper way to manage socket connections in Swift 3 using Singleton? 这是使用Singleton在Swift 3中管理套接字连接的正确方法吗?
Where do I start a socket connection such that I do not have to reconnect every time in the Viewcontroller lifecycle? 我在哪里开始套接字连接,这样我不必每次都在Viewcontroller生命周期中重新连接? The Viewcontroller is constantly sending data to my server;
Viewcontroller不断向我的服务器发送数据; my app is the client and its collecting data;
我的应用程序是客户端及其收集数据; its getting the data from movement within the app and sending direction, power, etc..
它从应用程序中移动数据并发送方向,电源等。
I have searched and searched but I cannot find a concrete example; 我搜索过,但找不到具体的例子; I only find socket libraries but not any examples on how to use it with UIViewcontroller;
我只找到套接字库,但没有任何关于如何在UIViewcontroller中使用它的示例; also, most examples online are using websockets, I am not
此外,大多数在线示例都使用websockets,我不是
Any suggestions? 有什么建议么?
I am hoping this will work: 我希望这会奏效:
manager.messageReceived(message: "Testing...")
but my fear is that inside the UIViewcontroller class, it will automatically start a connection again; 但我担心的是,在UIViewcontroller类中,它会自动重新启动连接; also, I am not sure at which point I should check if connection is lost and where to connect again;
另外,我不确定我应该检查连接是否丢失以及再次连接的位置; as I am never closing the socket
因为我永远不会关闭套接字
Thanks 谢谢
Singleton Class: 单身人士课程:
import Darwin
import Foundation
class ChatManager {
// Add property for socket
private var control_socket: Int32
private var address = "000.000.0.000"
private var port = "0000"
class var sharedInstance: ChatManager {
struct Singleton { static let instance = ChatManager() }
return Singleton.instance
}
init() {
// Create the socket
self.control_socket = connectToServer(atAddress: self.address, atPort: self.port)
}
func sendMessage(message: String) {
// Push the message onto the socket
_ = write(self.control_socket, message, message.characters.count)
}
// Delegate methods
func messageReceived(message: String) {
// Emit the message using NSNotificationCenter
}
}
UIViewcontroller: UIViewController中:
import Darwin
import Foundation
import UIKit
import Dispatch
class ViewController: UIViewController {
@IBOutlet private weak var joystickMove: Joystick!
@IBOutlet private weak var joystickRotate: Joystick!
private var contour = Contours()
private var contour_index: Int = 0
private let manager = ChatManager.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
createJoystick()
createContours()
createButton()
}
private func createJoystick() {
var joystick = Joystick()
let n: CGFloat = 100.0
let x: CGFloat = (UIScreen.main.bounds.width/2) - (n/2.0)
let y: CGFloat = UIScreen.main.bounds.height - (UIScreen.main.bounds.height/4.0)
joystick.frame = CGRect(x: x, y: y, width: n, height: n)
joystick.backgroundColor = UIColor.clear
joystick.substrateColor = UIColor.lightGray
joystick.substrateBorderColor = UIColor.gray
joystick.substrateBorderWidth = 1.0
joystick.stickSize = CGSize(width: 50.0, height: 50.0)
joystick.stickColor = UIColor.darkGray
joystick.stickBorderColor = UIColor.black
joystick.stickBorderWidth = 2.0
joystick.fade = 0.5
var packet = ""
joystick.trackingHandler = { (data) -> () in
let power = sqrt(pow(Double(data.velocity.x), 2.0) + pow(Double(data.velocity.y), 2.0))
let theta = atan2(Double(-data.velocity.y), Double(data.velocity.x))
let degrees = theta * (180.0 / M_PI)
if degrees >= 55 && degrees <= 125 { // move forward
packet = "\(1) \(1) \(power) \(power)"
} else if degrees >= -125 && degrees <= -55 { // move backewards
packet = "\(-1) \(-1) \(power) \(power)"
} else if degrees >= -55 && degrees <= 55 { // turn right
packet = "\(1) \(-1) \(power) \(power)"
} else if (degrees >= 125 && degrees <= 180) && (degrees >= -180 && degrees <= -125) { // turn left
packet = "\(-1) \(1) \(power) \(power)"
}
}
manager.messageReceived(message: "Testing...")
view.addSubview(joystick)
}
}
Since it is a singleton, have you considered initializing the Socket Manager in AppDelegate's application function? 既然它是单例,您是否考虑过在AppDelegate的应用程序函数中初始化套接字管理器? Then it will exist for the lifetime of the app.
然后它将存在于应用程序的生命周期中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.