Skip to content

Commit

Permalink
Merge pull request #4 from snakajima/push
Browse files Browse the repository at this point in the history
Push support
  • Loading branch information
snakajima committed Jan 7, 2021
2 parents e7ba2ac + 46b67b6 commit a832b71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 4 additions & 3 deletions core/BonjourConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CocoaAsyncSocket

public protocol BonjourConnectionDelegate : NSObjectProtocol {
// Only recponces not processed by callback (of send or call methos) will come here
func on(responce: BonjourResponse, connection: BonjourConnection)
func connection(connection: BonjourConnection, responce res: BonjourResponse, context: String?)
}

public class BonjourConnection: NSObject, ObservableObject {
Expand Down Expand Up @@ -108,12 +108,13 @@ extension BonjourConnection : GCDAsyncSocketDelegate {
do {
let result = try BonjourParser.parse(data)
let res = BonjourResponse(result: result)
if let context = res.headers["X-Context"],
let context = res.headers["X-Context"]
if let context = context,
let callback = callbacks[context] {
callback(res, res.jsonBody)
callbacks.removeValue(forKey: context)
} else {
delegate?.on(responce: res, connection: self)
delegate?.connection(connection: self, responce: res, context: context)
}
if let extraData = result.extraData {
print(" extra data", extraData.count)
Expand Down
9 changes: 7 additions & 2 deletions sampleClient/SampleHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import Foundation

class SampleHTTPClient: NSObject, BonjourConnectionDelegate {
func on(responce: BonjourResponse, connection: BonjourConnection) {
print("res", responce)
func connection(connection: BonjourConnection, responce res: BonjourResponse, context: String?) {
if let context = context, context == "push" {
print("pushed", res)

} else {
print("unhandled response", res)
}
}
}
9 changes: 9 additions & 0 deletions sampleServer/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ struct ContentView: View {
Text("Start")
})
}
Button(action: {
if let socket = myServer.clients.first {
var res = BonjourResponse(context: "push")
res.setBody(json: ["message":"I am pushing you"])
service.send(responce: res, to: socket)
}
}, label: {
Text("Push")
})
if let image = myServer.image {
Image(nsImage: NSImage(cgImage: image, size: CGSize(width: image.width, height: image.height)))
.resizable()
Expand Down

0 comments on commit a832b71

Please sign in to comment.