47 lines
1.2 KiB
HTML
47 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_a11233',//确保全局唯一
|
||
username:'emqx_test',
|
||
password:'emqx_test'
|
||
}
|
||
|
||
const connectUrl = 'ws://123.6.102.119:8083/mqtt'
|
||
const client = mqtt.connect(connectUrl, options)
|
||
|
||
client.on('connect', (error) => {
|
||
console.log('连接:', error)
|
||
|
||
client.subscribe('vehicle/#',{},function(e){//监听所有车辆数据,若仅仅监听位置:vehicle/position
|
||
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> |