forked from thingsboard/thingsboard.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request thingsboard#76 from dmytro-landiak/master
WebSocket API Example
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
|
||
<script type="text/javascript"> | ||
function WebSocketAPIExample() { | ||
var token = "YOUR_JWT_TOKEN"; | ||
var entityId = "YOUR_DEVICE_ID"; | ||
var webSocket = new WebSocket("ws(s)://host:port/api/ws/plugins/telemetry?token=" + token); | ||
|
||
if (entityId === "YOUR_DEVICE_ID") { | ||
alert("Invalid device id!"); | ||
webSocket.close(); | ||
} | ||
|
||
if (token === "YOUR_JWT_TOKEN") { | ||
alert("Invalid JWT token!"); | ||
webSocket.close(); | ||
} | ||
|
||
webSocket.onopen = function () { | ||
var object = { | ||
tsSubCmds: [ | ||
{ | ||
entityType: "DEVICE", | ||
entityId: entityId, | ||
scope: "LATEST_TELEMETRY", | ||
cmdId: 10 | ||
} | ||
], | ||
historyCmds: [], | ||
attrSubCmds: [] | ||
}; | ||
var data = JSON.stringify(object); | ||
webSocket.send(data); | ||
alert("Message is sent: " + data); | ||
}; | ||
|
||
webSocket.onmessage = function (event) { | ||
var received_msg = event.data; | ||
alert("Message is received: " + received_msg); | ||
}; | ||
|
||
webSocket.onclose = function (event) { | ||
alert("Connection is closed!"); | ||
}; | ||
} | ||
</script> | ||
|
||
</head> | ||
<body> | ||
|
||
<div id="sse"> | ||
<a href="javascript:WebSocketAPIExample()">Run WebSocket</a> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters