NanoMessage is a Swift-3/4 class wrapper of nanomsg
- Pair
- Push/Pull
- Request/Reply
- Publisher/Subscriber
- Surveyor/Respondent
- Bus
- TCP/IP
- In-Process Communication
- Inter-Process Communication
- Web Socket
- Send/Receive (Synchronous/Asynchronous) Blocking/Non-Blocking And Timeout
- Same message payload object for Send/Receive with message context
- Socket Polling (Single or Multiple)
- Device/Socket Binding (Synchronous/Asynchronous)
- Socket Loopback (Synchronous/Asynchronous)
If Swift Package Manager is used, add this package as a dependency in Package.swift
,
import PackageDescription
let package = Package (
name: "<your-app-name>",
dependencies: [
.Package(url: "https://github.com/itssofluffy/NanoMessage.git", majorVersion: 0)
]
)
Push
import Foundation
import NanoMessage
do {
guard let url = URL(string: "tcp://localhost:5555") else {
fatalError("url is not valid")
}
let node = try PushSocket()
let _: Int = try node.createEndPoint(url: url, type: .Connect)
try node.sendMessage(Message(value: "This is earth calling...earth calling..."),
timeout: TimeInterval(seconds: 10))
} catch let error as NanoMessageError {
print(error)
} catch {
print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}
Pull
import Foundation
import NanoMessage
do {
guard let url = URL(string: "tcp://*:5555") else {
fatalError("url is not valid")
}
let node = try PullSocket()
let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Bind, name: "my local end-point")
let received = try node.receiveMessage(timeout: TimeInterval(seconds: 10))
print("bytes : \(received.bytes)") // 40
print("message: \(received.message.string)") // This is earth calling...earth calling...
if (try node.removeEndPoint(endPoint)) {
print("end-point removed")
}
} catch let error as NanoMessageError {
print(error)
} catch {
print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}
Request
import Foundation
import NanoMessage
do {
guard let url = URL(string: "tcp://localhost:5555") else {
fatalError("url is not valid")
}
let node = try RequestSocket()
let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Connect, name: "request end-point")
usleep(TimeInterval(milliseconds: 200))
let timeout = TimeInterval(seconds: 10)
let message = Message(value: "ping")
let received = try node.sendMessage(message, sendTimeout: timeout, receiveTimeout: timeout) { sent in
print("sent \(sent)")
}
print("and recieved \(received)")
} catch let error as NanoMessageError {
print(error)
} catch {
print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}
Reply
import Foundation
import NanoMessage
do {
guard let url = URL(string: "tcp://*:5555") else {
fatalError("url is not valid")
}
let node = try ReplySocket()
let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Bind, name: "reply end-point")
usleep(TimeInterval(milliseconds: 200))
let timeout = TimeInterval(seconds: 10)
let sent = try node.receiveMessage(receiveTimeout: timeout, sendTimeout: timeout) { received in
print("received \(received)")
var message = ""
if (received.message.string == "ping") {
message = "pong"
} else {
message = "personally i prefer wiff-wafe"
}
return Message(value: message)
}
print("and sent \(sent)")
} catch let error as NanoMessageError {
print(error)
} catch {
print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}
This project is released under the MIT license. See LICENSE for details.