1
|
发送消息:
|
2
|
|
3
|
const WebSocket = require('ws');
|
4
|
|
5
|
// If set enableTLS to true, your have to set tlsEnabled to true in conf/websocket.conf.
|
6
|
const enableTLS = false;
|
7
|
const topic = `${enableTLS ? 'wss' : 'ws'}://localhost:8080/ws/v2/producer/persistent/public/default/my-topic`;
|
8
|
const ws = new WebSocket(topic);
|
9
|
|
10
|
var message = {
|
11
|
"payload" : new Buffer("Hello World").toString('base64'),
|
12
|
"properties": {
|
13
|
"key1" : "value1",
|
14
|
"key2" : "value2"
|
15
|
},
|
16
|
"context" : "1"
|
17
|
};
|
18
|
|
19
|
ws.on('open', function() {
|
20
|
// Send one message
|
21
|
ws.send(JSON.stringify(message));
|
22
|
});
|
23
|
|
24
|
ws.on('message', function(message) {
|
25
|
console.log('received ack: %s', message);
|
26
|
});
|
27
|
|
28
|
|
29
|
|
30
|
接收消息:
|
31
|
const WebSocket = require('ws');
|
32
|
|
33
|
// If set enableTLS to true, your have to set tlsEnabled to true in conf/websocket.conf.
|
34
|
const enableTLS = false;
|
35
|
const topic = `${enableTLS ? 'wss' : 'ws'}://localhost:8080/ws/v2/consumer/persistent/public/default/my-topic/my-sub`;
|
36
|
const ws = new WebSocket(topic);
|
37
|
|
38
|
ws.on('message', function(message) {
|
39
|
var receiveMsg = JSON.parse(message);
|
40
|
console.log('Received: %s - payload: %s', message, new Buffer(receiveMsg.payload, 'base64').toString());
|
41
|
var ackMsg = {"messageId" : receiveMsg.messageId};
|
42
|
ws.send(JSON.stringify(ackMsg));
|
43
|
});
|