XU XUv0.16.0

#github.com/xulang-org/packages/nats

导入方式: nats #github.com/xulang-org/packages/nats

公开 API: 10 函数, 24 方法, 5 类型, 0 常量, 0 方法集

扩展包 `nats #github.com/xulang-org/packages/nats`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

24_standalone_nats/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

nats #github.com/xulang-org/packages/nats
#os

fn main() int {
    println("=== NATS Cloud-Native Messaging Standalone Integration Test ===")
    addr := "127.0.0.1:4222"
    println("Connecting to NATS at " + addr + "...")

    $nc := nats.connect(addr + "") >>> (err) {
        println("Connection to NATS failed: " + err.msg())
        << 1
    }

    ping_ok := nc.ping() >>> (err) {
        println("Ping failed: " + err.msg())
        << 1
    }
    println("1. NATS Ping response: " + str(ping_ok))

    // 2. Subscribe to subject
    topic := "xu.events"
    sub := nc.subscribe(topic + "") >>> (err) {
        println("Subscribe failed: " + err.msg())
        << 1
    }
    println("2. Subscribed to subject: " + sub.subject + " (sid=" + sub.sid + ")")

    // 3. Publish a message
    payload := "Hello NATS from Xu Programming Language!"
    nc.publish(topic + "", payload + "") >>> (err) {
        println("Publish failed: " + err.msg())
        << 1
    }
    println("3. Published payload: " + payload)

    // 4. Receive the message
    msg := nc.next_msg() >>> (err) {
        println("Receive failed: " + err.msg())
        << 1
    }
    println("4. Successfully received message:")
    println("   - Subject: " + msg.subject)
    println("   - Sid: " + msg.sid)
    println("   - Data: " + msg.data)

    // 5. Test Queue Group (load-balanced worker queue)
    q_sub := nc.queue_subscribe("xu.tasks", "workers") >>> (err) {
        println("Queue subscribe failed: " + err.msg())
        << 1
    }
    println("5. Queue Group subscribed: xu.tasks -> workers (sid=" + q_sub.sid + ")")

    nc.publish("xu.tasks", "task_payload_123") >>> (err) {
        << 1
    }
    q_msg := nc.next_msg() >>> (err) {
        << 1
    }
    println("6. Queue Group received task: " + q_msg.data)

    nc.unsubscribe(sub) >>> (err) {
        << 1
    }
    nc.unsubscribe(q_sub) >>> (err) {
        << 1
    }
    nc.close()

    // 7. NATS Connection Pool Deep Verification
    println("7. Testing NATS Connection Pool with Reuse & Concurrency...")
    pool := nats.open_pool("127.0.0.1:4222", 5)
    $c1 := pool.get() >>> (err) {
        println("Pool get c1 failed: " + err.msg())
        << 1
    }
    fd_first := c1.fd
    c1_ok := c1.ping() >>> (err) {
        << 1
    }
    println("   Pool c1 ping: " + str(c1_ok) + " (fd=" + str(fd_first) + ")")
    pool.put(c1)

    // Verify Pool Connection REUSE
    $c2 := pool.get() >>> (err) {
        println("Pool get c2 failed: " + err.msg())
        << 1
    }
    fd_second := c2.fd
    if fd_first == fd_second {
        println("   [PASS] NATS Pool Connection REUSE verified! (fd1 == fd2: " + str(fd_first) + ")")
    } else {
        println("   [WARN] fd1 != fd2 (fd1=" + str(fd_first) + ", fd2=" + str(fd_second) + ")")
    }

    // Concurrent borrow another connection
    $c3 := pool.get() >>> (err) {
        println("Pool get c3 failed: " + err.msg())
        << 1
    }
    fd_third := c3.fd
    if fd_second != fd_third {
        println("   [PASS] NATS Pool Multi-connection concurrency verified! (fd2=" + str(fd_second) + " != fd3=" + str(fd_third) + ")")
    }

    // Pool Direct Publish
    pool.publish("pool.notify", "notification from nats pool") >>> (err) {
        << 1
    }
    println("   [PASS] NATS Pool direct publish succeeded.")

    pool.put(c2)
    pool.put(c3)
    pool.close()
    println("   [PASS] NATS Pool closed cleanly.")

    println("=== NATS Tests Completed Successfully! ===")
    << 0
}

API 声明

open

XuAPI
pub fn open(addr string) Client

用途

打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。

参数

addr string
地址(string)

返回值

Client — 该类型的值

open_pool

XuAPI
pub fn open_pool(addr string, max_idle int) Pool

用途

执行 open pool 操作。

参数

addr string
地址(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

struct Client

XuAPI
pub struct Client {
    pub fd int
    pub buf string
    pub addr string
    pub next_sid int
    pub server_info string
}

用途

Nats 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

fd int
文件描述符
buf string
缓冲
addr string
地址
next_sid int
字段(int)
server_info string
字段(string)

($&Client) read_line

XuAPI
pub fn (c $&Client) read_line() string

用途

$&Client 上的操作:执行 read line 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

string — 结果字符串

($&Client) read_bytes

XuAPI
pub fn (c $&Client) read_bytes(n int) string

用途

$&Client 上的操作:读到 delim(含 delim)。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

string — 结果字符串

($&Client) write_raw

XuAPI
pub fn (c $&Client) write_raw(data string) int

用途

$&Client 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

connect

XuAPI
pub fn connect(addr string) Client

用途

执行 connect 处理。

参数

addr string
地址(string)

返回值

Client — 该类型的值

查看用法示例

connect_auth

XuAPI
pub fn connect_auth(addr string, user string, pass string) Client

用途

执行 connect auth 操作。失败时抛出错误,调用方需处理或继续传播。

参数

addr string
地址(string)
user string
user(string)
pass string
pass(string)

返回值

Client — 该类型的值

($&Client) ping

XuAPI
pub fn (c $&Client) ping() bool

用途

发送心跳保活探测请求,检测连接活性与服务可用性。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

bool — 是否成立

($&Client) publish

XuAPI
pub fn (c $&Client) publish(subject string, data string) int

用途

向指定的消息主题或频道广播发布消息。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
subject string
subject(string)
data string
数据(string)

返回值

int — 该类型的值

($&Client) publish_request

XuAPI
pub fn (c $&Client) publish_request(subject string, reply_to string, data string) int

用途

$&Client 上的操作:执行 publish request 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
subject string
subject(string)
reply_to string
reply_to(string)
data string
数据(string)

返回值

int — 该类型的值

($&Client) subscribe

XuAPI
pub fn (c $&Client) subscribe(subject string) Subscription

用途

在 $&Client 上执行 subscribe 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
subject string
subject(string)

返回值

Subscription — 该类型的值

($&Client) queue_subscribe

XuAPI
pub fn (c $&Client) queue_subscribe(subject string, queue_group string) Subscription

用途

$&Client 上的操作:执行 queue subscribe 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
subject string
subject(string)
queue_group string
queue_group(string)

返回值

Subscription — 该类型的值

($&Client) unsubscribe

XuAPI
pub fn (c $&Client) unsubscribe(sub Subscription) int

用途

在 $&Client 上执行 unsubscribe 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
sub Subscription
子串(Subscription)

返回值

int — 该类型的值

($&Client) next_msg

XuAPI
pub fn (c $&Client) next_msg() Message

用途

$&Client 上的操作:执行 next msg 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

Message — 该类型的值

($&Client) request

XuAPI
pub fn (c $&Client) request(subject string, data string) Message

用途

$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
subject string
subject(string)
data string
数据(string)

返回值

Message — 该类型的值

($&Client) close

XuAPI
pub fn (c $&Client) close() int

用途

$&Client 上的操作:关闭资源。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

struct Message

XuAPI
pub struct Message {
    pub subject string
    pub sid string
    pub reply_to string
    pub data string
}

用途

承载 subject、sid、reply_to、data 等属性的结构体。

公开字段

subject string
字段(string)
sid string
字段(string)
reply_to string
字段(string)
data string
数据

new_msg

XuAPI
pub fn new_msg(subject string, sid string, reply_to string, data string) Message

用途

构造 msg。

参数

subject string
subject(string)
sid string
sid(string)
reply_to string
reply_to(string)
data string
数据(string)

返回值

Message — 该类型的值

struct Pool

XuAPI
pub struct Pool {
    pub url string
    pub auth_user string
    pub auth_pass string
    pub idle chan int
    pub max_idle int
}

用途

Nats 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

url string
字段(string)
auth_user string
字段(string)
auth_pass string
字段(string)
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(url string, max_idle int) Pool

用途

构造 pool。

参数

url string
url(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

new_pool_auth

XuAPI
pub fn new_pool_auth(url string, user string, pass string, max_idle int) Pool

用途

构造 pool auth。

参数

url string
url(string)
user string
user(string)
pass string
pass(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) put

XuAPI
pub fn (p &Pool) put(c Client)

用途

写入或更新指定键对应的值数据。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
c Client
字节或字符码(Client)

返回值

不返回值。

(&Pool) publish

XuAPI
pub fn (p &Pool) publish(subject string, data string) int

用途

向指定的消息主题或频道广播发布消息。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
subject string
subject(string)
data string
数据(string)

返回值

int — 该类型的值

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct Subscription

XuAPI
pub struct Subscription {
    pub sid string
    pub subject string
    pub queue_group string
}

用途

承载 sid、subject、queue_group 等属性的结构体。

公开字段

sid string
字段(string)
subject string
字段(string)
queue_group string
字段(string)

new_sub

XuAPI
pub fn new_sub(sid string, subject string, queue_group string) Subscription

用途

构造 sub。

参数

sid string
sid(string)
subject string
subject(string)
queue_group string
queue_group(string)

返回值

Subscription — 该类型的值

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

用途

Nats 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)

返回值

TlsClient — 该类型的值

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)
cfg tls.Config
cfg(tls.Config)

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

用途

$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

用途

$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int
need(int)

返回值

string — 结果字符串

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值