8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node-REDAdvent Calendar 2015

Day 19

node-red用websocketブリッヂ

Last updated at Posted at 2015-12-18

#概要
node-redのwebsocketは、sangoにつなげない。
sangoは、websocketにmqttが走っているようだ。
実証するため、以下のコードをjsdo.itで走らせた。
pahoを使った。
つながった。

#写真
websocket.jpg

#サンプルコード

var ws;
var ph;
function setup() {
    var userName = 'sango user';
    var password = 'sango pass';
    var websocketUrl = 'ws://lite.mqtt.shiguredo.jp:8080/mqtt';
    ws = new WebSocket("ws://node-red/test0");
    ws.onopen = onOpen;
    ws.onmessage = onMessage;
    ph = new Paho.MQTT.Client(websocketUrl, 'papo');
    ph.connect({
         userName: userName,
         password: password,
         onSuccess: onConnect,
         onFailure: failConnect
    });
    ph.onConnectionLost = onConnectionLost;
    ph.onMessageArrived = onMessageArrived;
}
function onOpen(e) {
    //alert("ws open");
};
function onMessage(e) {
    //alert("ws received data : " + e.data);
    ph.publish('sango user/test0', "ohisama");
};
function failConnect(e) {
    alert('ph ng0');
}
function onConnect() {
    //alert("ph connect");
    ph.subscribe('ohisama@github/test0');
}
function onMessageArrived(message) {
    //alert("ph recive msg");
    var val = message.payloadString;
    ws.send("ohisama");
}
function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) 
    {
        alert("ph onConnectionLost:" + responseObject.errorMessage);
    }
}
setup();
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?