Skip to content

创建PubSub实例 - PubSubNew

函数简介

创建 Pub/Sub 实例(客户端或服务端),支持 TCP 与 PRO 两种连接模式。

接口名称

PubSubNew

DLL调用

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=CLIENT2=SERVER
connect_type整数型连接类型:1=TCP2=PRO
ip字符串TCP 模式下客户端为目标地址、服务端为监听地址;PRO 模式可传空串。
port整数型TCP 端口;PRO 模式传 0
on_messagePubSubCallback消息回调,可为 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/2type 必须传 1/2connect_type 必须传 1/2(ABI 固定)。
创建后可调用 PubSubSub 订阅、`P创建后可调用 PubSubSub 订阅、PubSubPubText/PubSubPubBytes 发布。