Eight, the web version of the message push sdk-websockets

Source: Internet
Author: User

Introduced

As a result of the project team requirements, recently in the research message push service platform, combined with business and usage scenario analysis The final choice is Mosquitto messaging server, Mosquitto server installation, configuration, cluster construction I'm not here to say, interested can see me to Mosquitto The first seven articles of the study record. Today in this live to introduce the web version of WebSockets to implement the push of the message.

1: The development of this SDK is based on the Eclipse.paha.javaScript package.

2: Introducing JavaScript for development needs

<script src= "Https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type= "Text/javascript" > </script>

Note: This file can be downloaded to the official website, address: https://raw.githubusercontent.com/eclipse/paho.mqtt.javascript/master/src/mqttws31.js

3: Sub-assembly Realization JS-SDK

var websocket = function (config) {this.host = config.host;//ip this.port = config.port;//Port this.usetls = Config.usetls; Whether to enable TLS secure connection This.topic = config.topic;//subscription topicthis.username = config.username; Connection user name This.password = Config.password; Password this.cleansession = config.cleansession; False: Indicates that each time the same identity is logged in: true: Indicates that each time This.reconnecttimeout = 5000 is logged in as a new identity; Retry time-out this.clientid = Config.clientid; Client name var mqttclient;this.connect = function Mqttconnect () {mqttclient = new Paho.MQTT.Client (this.host, This.port, CLI  Entid); var options = {timeout:10,usessl:this.usetls,cleansession:this.cleansession,onsuccess:onconnect,onfailure: function (message) {Faildmssge (message.errormessage, "retrying", Fialdmessagecallback) setTimeout (Mqttconnect, this.reconnecttimeout);}}; Mqttclient.onconnectionlost = onconnectionlost;mqttclient.onmessagearrived = Onmessagearrived;if (this.userName! = NULL) {Options.username = This.username;options.password = This.password;} Console.log ("host=" + Host+ ", port=" + port + "TLS =" + usetls+ "username=" + username + "password=" + password); mqttclient.connect (options);} function OnConnect () {mqttclient.subscribe (topic, {qos:0});} function Onconnectionlost (response) {setTimeout (mqttconnect, this.reconnecttimeout); Faildmssge ("Connection lost:" + Responseobject.errormessage, ". Reconnecting ", Fialdmessagecallback)};function onmessagearrived (message) {var topic = Message.destinationname;var Payload = message.payloadstring;callback (topic, Payload, messagecallback);}; function callback (topic, payload, Messagecallback) {messagecallback (topic, payload);} function Faildmssge (errormessage, type, fialdmessagecallback) {fialdmessagecallback (errormessage, type);} This.sendmessage = function sendMessage (topic, message) {var message = new Paho.MQTT.Message (message); Message.destinationname = Topic;message.qos = 0;mqttclient.send (message);}}

4: Instructions for use

Create a config config = {host: ' 172.16.192.103 ', Port:9001,topic: ' # ', Usetls:false,username:null,password:nul L,cleansession:true,clientid: ' clientId '}//create a client Instancevar client = new WebSocket (config);//Connect the CL Ientclient.connect ();//send message client.sendmessage (topic, message);//Subscribe Topic message Callback Functionfunction messagecallback (topic, payload) {//Business logic processing}//error message callback Functionfunction Fialdmessagecallback (errormessage, type) {//Business logic processing}

To this web version of the message push basic functionality has been implemented: if you want a complete Web application instance, please go to the following address download: Https://github.com/yuelicn/mqtt-client.git

I have a limited level of JavaScript, if there is an inappropriate place to welcome my thanks!

Eight, the web version of the message push sdk-websockets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.