摸索了一天,网上和官网的版本均是基于Swift3的,很多模块都没有更新,导致无法使用。这里提供一个5.1的教程。
添加dependencies
dependencies: [
.package(url: "https://github.com/Linzecong/LP_ZMQ.git", .branch("master"))
],
更新了一下代码Swift3.0的代码,使得兼容Swift5.1
求个赞 https://github.com/Linzecong/LP_ZMQ
安装步骤
Installation
sudo apt-get update
sudo apt-get install build-essential pkg-config
cd /tmp/
sudo curl -L -O https://github.com/zeromq/zeromq4-1/releases/download/v4.1.4/zeromq-4.1.4.tar.gz
sudo tar xf /tmp/zeromq-4.1.4.tar.gz
cd /tmp/zeromq-4.1.4
sudo ./configure --without-libsodium
sudo make
sudo make install
在Package.swift添加依赖
dependencies: [
...
.package(url: "https://github.com/Linzecong/LP_ZMQ.git", .branch("master"))
],
使用教程
关于ZMQ的概念,推荐一个教程!https://legacy.gitbook.com/book/wizardforcel/zmq-guide/details
REQ — REP 模式
这个模式就是一问一答模式,每个请求必须要收到一个回复,才能继续进行下一个请求
客户端
import LP_ZMQ
let context = try Context()
let clientsock = try context.socket(.req)
try clientsock.connect("tcp://127.0.0.1:5558")
try clientsock.send("hello")
var data = try clientsock.receive()
print(data)
try clientsock.send("hello")
data = try clientsock.receive()
print(data)
服务端
import LP_ZMQ
let context = try Context()
let serversock = try context.socket(.rep)
try serversock.bind("tcp://*:5558")
while true{
let data = try serversock.receive()
print(data)
try serversock.send("world")
}
PUSH — PULL 模式
这个模式不需要一问一答,就是字面意思,消息会存在队列里,直到被pull为止
客户端
import LP_ZMQ
let context = try Context()
let clientsock = try context.socket(.push)
try clientsock.connect("tcp://127.0.0.1:5558")
try clientsock.send("hello")
try clientsock.send("hello")
try clientsock.send("hello")
服务端
import LP_ZMQ
let context = try Context()
let serversock = try context.socket(.pull)
try serversock.bind("tcp://*:5558")
while true{
let data = try serversock.receive()
print(data)
}
PUB — SUB 模式
发布者和订阅者模式!可以有一个发布者,多个订阅者~,不同于push pull,消息发送不会堆积。
客户端
import LP_ZMQ
let context = try Context()
let clientsock = try context.socket(.sub)
try clientsock.connect("tcp://127.0.0.1:5558")
try clientsock.setSubscribe(Data("A".utf8)) // 设置消息过滤,仅接受A开头的数据
while true{
let data = try clientsock.receive()
print(data)
}
服务端
import LP_ZMQ
import Foundation
let context = try Context()
let serversock = try context.socket(.pub)
try serversock.bind("tcp://*:5558")
while true{
Thread.sleep(forTimeInterval: 1)
//try serversock.send("A",mode:[SendMode.SendMore])
try serversock.send(String("A123"))
}
ROUTER — DEALER模式
路由模式,通过设置Identity来决定发往哪个处理者,可以实现均衡负载。
客户端A
import LP_ZMQ
let context = try Context()
let clientsock = try context.socket(.dealer)
try clientsock.setIdentity("A") //仅接受A路由的消息
try clientsock.connect("tcp://127.0.0.1:5558")
while true{
let data = try clientsock.receive()
print("A", data)
}
客户端B
import LP_ZMQ
let context = try Context()
let clientsock = try context.socket(.dealer)
try clientsock.setIdentity("B") //仅接受B路由的消息
try clientsock.connect("tcp://127.0.0.1:5558")
while true{
let data = try clientsock.receive()
print("B", data)
}
服务端
import LP_ZMQ
import Foundation
let context = try Context()
let serversock = try context.socket(.router)
try serversock.bind("tcp://*:5558")
while true{
Thread.sleep(forTimeInterval: 1)
try serversock.send("A",mode:[SendMode.SendMore]) //发往A路由的消息
try serversock.send(String("to A"))
try serversock.send("B",mode:[SendMode.SendMore]) //发往B路由的消息
try serversock.send(String("to B"))
}