48 lines
1.2 KiB
HTML
48 lines
1.2 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="en">
|
|||
|
<head>
|
|||
|
<meta charset="UTF-8">
|
|||
|
<title>通过socket接收实时车辆数据(假车)</title>
|
|||
|
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
|
|||
|
<script>
|
|||
|
|
|||
|
const options ={
|
|||
|
clientId:'mqttjs_a13',
|
|||
|
username:'emqx_test',
|
|||
|
password:'emqx_test'
|
|||
|
}
|
|||
|
|
|||
|
const connectUrl = 'ws://192.168.3.112:8083/mqtt'
|
|||
|
const client = mqtt.connect(connectUrl, options)
|
|||
|
|
|||
|
client.on('connect', (error) => {
|
|||
|
console.log('连接:', error)
|
|||
|
|
|||
|
client.subscribe('vehicle/sim/data',{},function(e){
|
|||
|
console.log(e)
|
|||
|
})
|
|||
|
})
|
|||
|
|
|||
|
client.on('reconnect', (error) => {
|
|||
|
console.log('正在重连:', error)
|
|||
|
})
|
|||
|
|
|||
|
client.on('error', (error) => {
|
|||
|
console.log('连接失败:', error)
|
|||
|
})
|
|||
|
|
|||
|
client.on('message', (topic, message) => {
|
|||
|
console.log('收到消息:', topic, message.toString())
|
|||
|
})
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</script>
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
假车
|
|||
|
<a href="https://docs.emqx.com/zh/emqx/latest/connect-emqx/javascript.html">使用 JavaScript SDK 连接EMQX</a>
|
|||
|
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|