gpt4 book ai didi

ios - Swift 套接字

转载 作者:行者123 更新时间:2023-11-29 02:18:22 30 4
gpt4 key购买 nike

我正在使用套接字创建我的 Swift 程序,我想知道是否需要使用 GCDAsyncSocket 库?我正在尝试创建一个将字符串发送到我的 mac os x 应用程序的 iOS 应用程序。我以前用 Java 编写过一个与此类似的套接字应用程序,所以我对套接字非常熟悉。我的服务器端(mac os)代码看起来像这样,我不确定如何处理客户端(iOS 端)

var bsocket: GCDAsyncSocket!

func applicationDidFinishLaunching(aNotification: NSNotification) {
bsocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
var port:UInt16 = 8090
if (!bsocket.connectToHost("localhost", onPort: port, error: nil))
{
println("Error")
}
else
{
println("Connecting...")
}
}

func socket(socket : GCDAsyncSocket, didReadData data:NSData, withTag tag:UInt16)
{
var response = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Received Response")
}

func socket(socket : GCDAsyncSocket, didConnectToHost host:String, port p:UInt16)
{
println("Connected to \(host) on port \(p).")

var request:String = "Welcome to the server."
var data:NSData = request.dataUsingEncoding(NSUTF8StringEncoding)!
bsocket.writeData(data, withTimeout: -1.0, tag: 0)
bsocket.readDataWithTimeout(-1.0, tag: 0)
}

最佳答案

本教程来自 Ray Wenderlich.com http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server对连接到基本服务器套接字所需的客户端有很好的描述。它不执行 websocket 协议(protocol)中所需的任何握手、心跳和帧,因此它与 websockets 不兼容。您的代码没有显示 websocket 协议(protocol)所需的任何握手,所以我认为这就是您想要的。如果您需要 websockets,请查看 https://github.com/daltoniam/Starscream

这是我对 Swift 解释的基础知识。我最终得到了一个经理和一个连接:

class Manager : NSObject {
var conn = Connection()

func connect() {
let (host, port) = screen.getAddress()
conn.connect(host, port: port)
}

func disconnect() {
conn.disconnect()
}

func sendMessage(params:[String : AnyObject]) {
let msg = "send_message:" + JSONStringify(params)

let data : NSData = msg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
var buffer = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&buffer)

conn.outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
}

func JSONStringify(value: AnyObject) -> String {
if NSJSONSerialization.isValidJSONObject(value) {
if let data = NSJSONSerialization.dataWithJSONObject(value, options: nil, error: nil) {
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
return string
}
}
}
return ""
}
}

class Connection : NSObject, NSStreamDelegate {
var serverAddress: CFString = "127.0.0.1"
var serverPort: UInt32 = 8443

private var inputStream: NSInputStream!
private var outputStream: NSOutputStream!

func connect(address: CFString, port:UInt32) {
println("connecting...")

var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?

CFStreamCreatePairWithSocketToHost(nil, address, port, &readStream, &writeStream)

// Documentation suggests readStream and writeStream can be assumed to
// be non-nil. It might be wise to test if either is nil
// and implement error-handling as needed.

self.inputStream = readStream!.takeRetainedValue()
self.outputStream = writeStream!.takeRetainedValue()

self.inputStream.delegate = self
self.outputStream.delegate = self

self.inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

self.inputStream.open()
self.outputStream.open()
}

func disconnect() {
self.inputStream.close()
self.outputStream.close()
}


func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
switch (eventCode){
case NSStreamEvent.ErrorOccurred:
NSLog("ErrorOccurred")
case NSStreamEvent.EndEncountered:
NSLog("EndEncountered")
case NSStreamEvent.None:
NSLog("None")
case NSStreamEvent.HasBytesAvailable:
NSLog("HasBytesAvaible")
var buffer = [UInt8](count: 4096, repeatedValue: 0)
while (inputStream.hasBytesAvailable){
var len = inputStream.read(&buffer, maxLength: buffer.count)
if(len > 0){
var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
if (output != ""){
NSLog("server said: %@", output!)
}
} else {
println("empty string from stream")
}
}
case NSStreamEvent.allZeros:
NSLog("allZeros")
case NSStreamEvent.OpenCompleted:
NSLog("OpenCompleted")
case NSStreamEvent.HasSpaceAvailable:
NSLog("HasSpaceAvailable")
default: println("default reached. unknown stream event")
}
}
}

您可能还会发现这些链接对 Swift 中的消息传递很有用:

我不确定这段代码中的 JSON 转换是否正常工作。我发现我需要websockets,所以不再使用这个。

关于ios - Swift 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466665/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com