跳到主要内容

Saydo REST示例代码

以下各节提供了一些发送和接收 REST 消息的示例代码。

使用CURL向代理发送消息

向主题 “a” 发送消息(两个命令等效):

curl -X POST -d "Hello World Topic" http://<ip:port>/a --header "Content-Type: text/plain"
curl -X POST -d "Hello World Topic" http://<ip:port>/TOPIC/a --header "Content-Type: text/plain"

向队列 “Q/test” 发送消息:

curl -X POST -d "Hello World Queue" http://<ip:port>/QUEUE/Q/test --header "Content-Type: text/plain"

在主题 “rr” 上向 Solace 发送请求/回复消息,超时时间为 30 秒,Solace 相关 ID 为 'x':

curl -X POST -d "Hello World Request Reply" http://<ip:port>/TOPIC/rr --header "Solace-Reply-Wait-Time-In-ms:30000" --header "Solace-Correlation-ID:x"

使用JavaScript向代理发送消息

以下是一个使用 JavaScript 和 Node.js 向主题 “a/b/c” 发送消息的示例:

var http = require('http');

var userString = "Hello World JavaScript";

var headers = {
'Content-Type': 'text/plain',
'Content-Length': userString.length
};

var options = {
host: '<HOST>',
port: <PORT>,
path: '/TOPIC/a/b/c',
method: 'POST',
headers: headers
};

// 设置请求。options 参数是我们上面定义的对象。
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.write(userString);
req.end();

必须将 <HOST><PORT> 更新为指向事件代理的地址。

使用JavaScript从代理接收消息

以下代码是一个极简的 HTTP 服务器,可以使用 Node.js 运行。它将对所有传入请求以 200 OK 和 “Hello World Response” 作为 HTTP 响应正文进行响应。

var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World Response\n');
}).listen(<PORT>, '<HOST>');
console.log('Server running at http://<HOST>:<PORT>/');

在将队列绑定配置到此服务器时,必须使用有效的路径(例如 “/”)作为 POST 请求目标。