实现一个 websocket 服务并不难,但是要做到弹性伸缩,而且长期空转实属有点亏。用阿里云 mqtt serverless 来开发就非常方便。每天成本几块钱即可。
我的业务场景是这样:给网页客户端推送一个消息,那么比较适用的就是 mqtt p2p 模式。
前端
注意千万别用官方文档里面的demo,因为会造成 ak 泄漏,需要使用 一机一密方案
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Aliyun Mqtt Websockets</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js" type="text/javascript"></script>
<script type="text/javascript">
instanceId = 'post-cn-x0r3oqq4d04';//实例 ID,购买后从控制台获取
host = 'post-cn-x0r3oqq4d04.mqtt.aliyuncs.com';// 设置当前用户的接入点域名,接入点获取方法请参考接入准备章节文档,先在控制台创建实例
port = 80;//WebSocket 协议服务端口,如果是走 HTTPS,设置443端口
topic = 'demo';//需要操作的 Topic,第一级父级 topic 需要在控制台申请
useTLS = false;//是否走加密 HTTPS,如果走 HTTPS,设置为 true
accessKey = 'DC.xxx';
//账号的的 SecretKey,在阿里云控制台查看
secretKey = 'DC.xxx';
cleansession = true;
groupId = 'GID_test';//MQTT GroupID,创建实例后从 MQTT 控制台创建
clientId = groupId + '@@@1002';//GroupId@@@DeviceId,由控制台创建的 Group ID 和自己指定的 Device ID 组合构成
var mqtt;
var reconnectTimeout = 2000;
var username = 'DeviceCredential|' + accessKey + '|' + instanceId;
var password = CryptoJS.HmacSHA1(clientId, secretKey).toString(CryptoJS.enc.Base64);
function MQTTconnect() {
mqtt = new Paho.MQTT.Client(
host,//MQTT 域名
port,//WebSocket 端口,如果使用 HTTPS 加密则配置为443,否则配置80
clientId//客户端 ClientId
);
var options = {
timeout: 3,
onSuccess: onConnect,
mqttVersion: 4,
cleanSession: cleansession,
onFailure: function (message) {
setTimeout(MQTTconnect, reconnectTimeout);
}
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
if (username != null) {
options.userName = username;
options.password = password;
options.useSSL = useTLS;//如果使用 HTTPS 加密则配置为 true
}
mqtt.connect(options);
}
function onConnect() {
console.log("连接成功");
}
function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
};
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
console.log("recv msg : " + topic + " " + payload);
};
MQTTconnect();
</script>
</head>
</html>
这里的 accessKey 和 secretKey 的生成,依赖自己的后端服务返回,原理在 https://help.aliyun.com/zh/apsaramq-for-mqtt/developer-reference/overview-of-unique-certificate-per-device-authentication
后端调用 RegisterDeviceCredential - 为某个设备注册访问凭证 支持在线调试,非常方便。
后端代码
String receiverId = "GID_test@@@1002";
final String p2pSendTopic = parentTopic + "/p2p/" + receiverId;
MqttMessage message = new MqttMessage("hello mq4Iot p2p msg".getBytes());
message.setQos(qosLevel);
mqttClient.publish(p2pSendTopic, message);