简体   繁体   English

无法运行MQTT JavaScript客户端

[英]Trouble to get MQTT JavaScript Client running

I try to get a MQTT JavaScript Client running. 我尝试运行MQTT JavaScript客户端。 It's based on the Eclipse Paho Client Library (org.eclipse.paho.mqtt.javascript.git). 它基于Eclipse Paho客户端库(org.eclipse.paho.mqtt.javascript.git)。

Before running the JavaScript Client I was performing some tests with 在运行JavaScript客户端之前,我正在执行一些测试

  • mosquitto_pub -h test.mosquitto.org -t "/topic1" -m "test" mosquitto_pub -h test.mosquitto.org -t“/ topic1”-m“test”

and

  • mosquitto_sub -h test.mosquitto.org -t "/topic1" -v mosquitto_sub -h test.mosquitto.org -t“/ topic1”-v

which are working fine. 工作正常。

Then I called my own mqttTest.html which contains: 然后我打电话给我自己的mqttTest.html,它包含:

<!DOCTYPE html>
<head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  <script type="text/JavaScript" src="mqttws31.js"></script>
  <script type="text/JavaScript">

  var client;

  function doConnect() {

    client = new Messaging.Client("test.mosquitto.org", 1883, "mosqOtti");
    console.log("Client instantiated.");
    client.startTrace();
    console.log("Now trying to connect...");
    client.connect({onSuccess:onConnect});

  }

  function onConnect() {

    console.log("connection established");
    doSubscribe();

  }

  function doSubscribe() {

      client.subscribe("/topic1");

  }

  window.onload = function() {

      this.doConnect();

  }

</script>
</head> 

.
.
.

</body>
</html>

I tried to lauch this in Firefox. 我试图在Firefox中实现这一点。 The debug console output tells me that 调试控制台输出告诉我

[09:58:27.825] Firefox can't establish a connection to the server at ws://test.mosquitto.org:1883/mqtt. @ file:///mqttws31.js:914

I know that moquitto does not support websockets natively. 我知道moquitto本身不支持websockets。 But I red that the lighttp running on test.mosquitto.org has mod_websockets installed. 但我认为test.mosquitto.org上运行的lighttp已经安装了mod_websockets。

Line 914 of mqttws31.js is trying to do this.socket = new WebSocket(wsurl, 'mqttv3.1'); mqttws31.js的第914行正在尝试执行此操作.socket = new WebSocket(wsurl,'mqttv3.1');

So it seems that 所以看来

  • either websockets doesn't really work for test.mosquitto.org websockets并不适用于test.mosquitto.org
  • or my example is buggy! 或者我的例子是马车!

I stuggled around for a long time now and need to get a JavaScript MQTT Client running. 我现在徘徊了很长时间,需要运行一个JavaScript MQTT客户端。

Does anyone have an idea? 有没有人有想法? Or another approach? 还是另一种方法? Socket.IO seems not to be the right solution too. Socket.IO似乎也不是正确的解决方案。

Thanks very much in advance! 首先十分感谢!

As @hardillb says, the port you are using is incorrect. 正如@hardillb所说,您使用的端口不正确。 1883 on test.mosquitto.org is solely for mqtt. test.mosquitto.org上的1883年仅供mqtt使用。 If you wish to use websockets you need to connect using port 80. You should just be able to change your url to ws://test.mosquitto.org:1883/mqtt which presumably means changing your code to 如果你想使用websockets,你需要使用端口80连接。你应该只能将你的网址更改为ws://test.mosquitto.org:1883/mqtt ,这可能意味着你的代码更改为

client = new Messaging.Client("test.mosquitto.org", 80, "mosqOtti");

There is a websockets example (based on this code ) running at http://test.mosquitto.org/sys/ . http://test.mosquitto.org/sys/上运行了一个websockets示例(基于此代码 )。 Although it uses the deprecated mosquitto javascript client, it should demonstrate that it works. 虽然它使用了已弃用的mosquitto javascript客户端,但它应该证明它有效。

The lighttpd config on test.mosquitto.org is: test.mosquitto.org上的lighttpd配置是:

websocket.server = (
    "/mqtt" =>
    (  
        "host" => "127.0.0.1",
        "port" => "1883",
        "subproto" => "mqttv3.1",
        "type" => "bin"
    )
)

I had a lot of trouble getting this to work for me and wanted to post solution for Ubuntu that was quickest/easiest for me. 我很难让这个为我工作,并想为Ubuntu发布最快/最简单的解决方案。

To install lighttpd with websocket support on Ubuntu 在Ubuntu上安装带有websocket支持的lighttpd

Basically, follow this blog post: http://oriolrius.cat/blog/tag/mqtt/ 基本上,请关注此博文: http//oriolrius.cat/blog/tag/mqtt/

Steps: 脚步:

1) Add repository for your version of ubuntu from here: https://launchpad.net/~roger.light/+archive/ppa/ 1)从这里为您的ubuntu版本添加存储库: https//launchpad.net/~roger.light/+archive/ppa/

For Ubuntu 12.04 LTS (Precise), the lines are: 对于Ubuntu 12.04 LTS(精确),线路是:

deb http://ppa.launchpad.net/roger.light/ppa/ubuntu precise main
deb-src http://ppa.launchpad.net/roger.light/ppa/ubuntu precise main

2) Install lighttpd and mod_websocket plugin: 2)安装lighttpd和mod_websocket插件:

apt-get update
apt-get install lighttpd lighttpd-mod-websocket

3) Add configuration for MQTT websocket somewhere that's included by /etc/lighttpd/lighttpd.conf 3)在/etc/lighttpd/lighttpd.conf中包含的某个地方添加MQTT websocket的配置

server.modules = ( "mod_websocket", )

websocket.server = (
    "/mqtt" => (
        "host" => "127.0.0.1",
        "port" => "1883",
        "type" => "bin",
        "subproto" => "mqttv3.1"
    ),
)

...

4) Optionally, point lighttpd at non-html socket. 4)(可选)将lighttpd指向非html套接字。 This worked 这很有效

server.port = 8080

5) Restart lighttpd, and Javascript client connects to http://hostname:8080 . 5)重启lighttpd,Javascript客户端连接到http://hostname:8080 I used Paho Javascript client following commands represented here: https://www.ibm.com/developerworks/community/blogs/c565c720-fe84-4f63-873f-607d87787327/entry/how_to_prog_javascript?lang=en 我使用了Paho Javascript客户端,其中包含以下命令: https ://www.ibm.com/developerworks/community/blogs/c565c720-fe84-4f63-873f-607d87787327/entry/how_to_prog_javascript?lang = en

service lighttpd restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM