2016-04-01 18:31:07 +08:00
|
|
|
/**
|
2016-05-19 06:26:08 +08:00
|
|
|
* Copyright 2015, 2016 IBM Corp.
|
2016-04-01 18:31:07 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var express = require("express");
|
2016-05-19 06:26:08 +08:00
|
|
|
var io = require('socket.io')(RED.server);
|
2016-04-01 18:31:07 +08:00
|
|
|
|
|
|
|
var WorldMap = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2016-05-19 06:26:08 +08:00
|
|
|
var node = this;
|
|
|
|
//node.log("Serving map from "+__dirname+" as "+RED.settings.httpNodeRoot.slice(0,-1)+"/worldmap");
|
2016-04-01 18:31:07 +08:00
|
|
|
RED.httpNode.use("/worldmap", express.static(__dirname + '/worldmap'));
|
2016-05-19 06:26:08 +08:00
|
|
|
io.on('connection', function(socket) {
|
2016-05-27 05:12:56 +08:00
|
|
|
//node.log(socket.request.connection.remoteAddress);
|
2016-05-25 04:34:18 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2016-05-19 06:26:08 +08:00
|
|
|
node.on('input', function(msg) {
|
|
|
|
socket.emit("worldmapdata",msg.payload);
|
|
|
|
});
|
2016-05-25 04:34:18 +08:00
|
|
|
socket.on('disconnect', function() {
|
|
|
|
node.status({fill:"red",shape:"ring",text:"disconnected"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
node.on("close", function() {
|
|
|
|
node.status({});
|
2016-05-19 06:26:08 +08:00
|
|
|
});
|
2016-05-27 05:12:56 +08:00
|
|
|
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap",WorldMap);
|
2016-05-19 06:26:08 +08:00
|
|
|
|
|
|
|
var WorldMapIn = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
|
|
|
io.on('connection', function(socket) {
|
2016-05-25 04:34:18 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2016-05-19 06:26:08 +08:00
|
|
|
socket.on('worldmap', function(data) {
|
|
|
|
node.send({payload:data, topic:"worldmap"});
|
|
|
|
});
|
|
|
|
socket.on('disconnect', function() {
|
2016-05-25 04:34:18 +08:00
|
|
|
node.status({fill:"red",shape:"ring",text:"disconnected"});
|
2016-05-19 06:26:08 +08:00
|
|
|
node.send({payload:{action:"disconnect"}, topic:"worldmap"});
|
|
|
|
});
|
|
|
|
});
|
2016-05-25 04:34:18 +08:00
|
|
|
node.on("close", function() {
|
|
|
|
node.status({});
|
|
|
|
});
|
2016-05-19 06:26:08 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap in",WorldMapIn);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|