主题
订阅主题 - PubSubSub
函数简介
将当前 Pub/Sub 句柄订阅到指定主题。
接口名称
PubSubSubDLL调用
c
int32_t PubSubSub(int64_t instance, int64_t client, char* topic);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLA 实例句柄。 |
| client | 长整数型 | Pub/Sub 句柄。 |
| topic | 字符串 | 主题名(非空)。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// type=1 CLIENT, connect_type=1 TCP;回调传 nullptr 表示仅发布
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, nullptr);
if (pubsub > 0) {
ola.PubSubSub(pubsub, "topic/demo");
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)
{
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, None)
if pubsub > 0:
ola.PubSubSub(pubsub, "topic/demo")
ola.PubSubFree(pubsub)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, null);
if (pubsub > 0) {
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub);
}cpp
var ola = com("OlaPlug.OlaSoft")
var pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0)
if(pubsub > 0) {
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0)
If pubsub > 0 Then
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
pubsub = ola.PubSubNew (1, 1, "127.0.0.1", 18890, 0)
.如果真 (pubsub > 0)
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree (pubsub)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, null);
if(pubsub > 0){
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, 0)
如果真 (pubsub > 0)
{
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long pubsub = ola.PubSubNew(1, 1, "127.0.0.1", 18890, nullptr);
if (pubsub > 0) {
ola.PubSubSub(pubsub, "topic/demo");
ola.PubSubFree(pubsub);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long pubsub = PubSubNew(instance, 1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
PubSubSub(instance, pubsub, "topic/demo");
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);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int PubSubSub(long ola, long client, string topic);
long instance = CreateCOLAPlugInterFace();
long pubsub = PubSubNew(instance, 1, 1, "127.0.0.1", 18890, 0);
if (pubsub > 0) {
PubSubSub(instance, pubsub, "topic/demo");
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()
pubsub = ola.PubSubNew(instance, 1, 1, b"127.0.0.1", 18890)
if pubsub:
ola.PubSubSub(instance, pubsub, "topic/demo");
ola.PubSubFree(instance, pubsub)返回值
| 返回值 | 说明 |
|---|---|
0 | 成功。 |
| < 0 | 错误码,失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 订阅成功后 | 订阅成功后,消息会通过创建时的 on_message 回调送达。 |
