Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
- <script
- src="https://code.jquery.com/jquery-2.2.4.min.js"
- integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
- crossorigin="anonymous"></script>
- <script>
- $(function() {
- $("#send-msg").click(send);
- // Create a client instance
- client = new Paho.MQTT.Client("pcfeib425t.vsb.cz", Number(9999),"/ws", "mer0061");
- // set callback handlers
- client.onConnectionLost = onConnectionLost;
- client.onMessageArrived = onMessageArrived;
- let will = new Paho.MQTT.Message("Will msg from mer0061, cya");
- will.destinationName = "/mschat/all/mer0061";
- // connect the client
- client.connect({
- onSuccess:onConnect,
- onFailure: onFail,
- userName: "mobilni",
- password: "Systemy",
- willMessage: will
- });
- function send() {
- console.log("sending");
- let msg = new Paho.MQTT.Message(Math.floor(Date.now() / 1000) + " " + $("#msg").val());
- $("#msg").val("");
- msg.destinationName = "/mschat/all/mer0061";
- client.send(msg);
- }
- function onFail(ctx, code, msg) {
- console.log("Failed to connect: " + msg);
- }
- // called when the client connects
- function onConnect() {
- // Once a connection has been made, make a subscription and send a message.
- console.log("onConnect");
- client.subscribe("/mschat/#");
- //message.destinationName = "/mschat/all/mer0061";
- //message = new Paho.MQTT.Message(Math.floor(Date.now() / 1000) + " -> Hello");
- //client.send(message);
- }
- // called when the client loses its connection
- function onConnectionLost(responseObject) {
- if (responseObject.errorCode !== 0) {
- console.log("onConnectionLost:"+responseObject.errorMessage);
- }
- }
- // called when a message arrives
- function onMessageArrived(message) {
- $("#messages").append($(`<div>${message.payloadString}</div>`))
- console.log("onMessageArrived:"+message.payloadString);
- }
- });
- </script>
- </head>
- <body>
- <div id="messages">
- </div>
- <input type="text" id="msg" />
- <button id="send-msg">Send</button>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement