主题
创建PubSub实例 - PubSubNew
函数简介
创建 Pub/Sub 实例(客户端或服务端),支持 TCP 与 PRO 两种连接模式。
接口名称
PubSubNewDLL调用
c
int64_t PubSubNew(int64_t instance, int32_t type, int32_t connect_type,
char* ip, int32_t port, PubSubCallback on_message);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLA 实例句柄。 |
| type | 整数型 | 角色类型:1=CLIENT,2=SERVER。 |
| connect_type | 整数型 | 连接类型:1=TCP,2=PRO。 |
| ip | 字符串 | TCP 模式下客户端为目标地址、服务端为监听地址;PRO 模式可传空串。 |
| port | 整数型 | TCP 端口;PRO 模式传 0。 |
| on_message | PubSubCallback | 消息回调,可为 NULL(仅发布不消费)。 |
PubSubCallback 回调函数定义
c
typedef void(OLA_CALL_TYPE* PubSubCallback)(int64_t client, char* topic,
int64_t data_ptr, int32_t data_len, int32_t is_text);| 参数名 | 类型 | 说明 |
|---|---|---|
| client | 长整数型 | 客户端句柄。 |
| topic | 字符串 | 主题名。 |
| data_ptr | 长整数型 | 数据指针(仅回调期间有效)。 |
| data_len | 整数型 | 数据长度(字节)。 |
| is_text | 整数型 | 是否文本:1=文本,0=字节流。 |
回调使用说明
data_ptr仅在回调执行期间有效,若需异步处理请先拷贝数据。is_text=1时可按字符串处理,is_text=0时按二进制处理。
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, nullptr);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, null);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
// type=1 CLIENT, connect_type=1 TCP
pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, None)
if pubsub > 0:
# pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, null);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}cpp
var ola = com("OlaPlug.OlaSoft")
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}text
.局部变量 ola, OLAPlug
ola.创建 ()
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, null);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// type=1 CLIENT, connect_type=1 TCP
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, nullptr);
if (pubsub > 0) {
// pubsub 为 Pub/Sub 句柄
ola.PubSubFree(pubsub);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long pubsub = PubSubNew(instance, 1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) PubSubFree(instance, pubsub);csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long PubSubNew(long ola, int type, int connect_type, string ip, int port, long callback);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int PubSubFree(long ola, long client);
long instance = CreateCOLAPlugInterFace();
long pubsub = PubSubNew(instance, 1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
PubSubFree(instance, pubsub);
}python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
long pubsub = PubSubNew(instance, 1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) PubSubFree(instance, pubsub);返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 大于 0 为实例句柄,<=0 为错误码。 |
注意事项
| 项目 | 说明 |
|---|---|
type 必须传 1/2 | type 必须传 1/2,connect_type 必须传 1/2(ABI 固定)。 |
创建后可调用 PubSubSub 订阅、`P | 创建后可调用 PubSubSub 订阅、PubSubPubText/PubSubPubBytes 发布。 |
