2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an AS IS BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 var fs = require('fs');
\r
18 var http = require('http');
\r
20 function extractPath(url) {
\r
21 var urlParts = url.split('/'),
\r
23 l = urlParts.length,
\r
25 for (; i < l; ++i) {
\r
26 if (urlParts[i].length > 0) {
\r
27 result.push(urlParts[i]);
\r
33 http.createServer(function(req, res) {
\r
34 req.on('end', function() {
\r
35 var path = extractPath(req.url);
\r
36 console.log(req.url)
\r
37 // var last = path[path.length - 1];
\r
38 if (path[0] === undefined) {
\r
40 res.end(fs.readFileSync('public/index.html'));
\r
41 } else if (path[0] == 'test') {
\r
43 res.end(fs.readFileSync('public/test.html'));
\r
44 } else if (req.url == '/js/app.js') {
\r
46 res.end(fs.readFileSync('public/js/app.js'));
\r
47 } else if (req.url == '/css/style.css') {
\r
49 res.end(fs.readFileSync('public/css/style.css'));
\r
51 res.setHeader('Location', 'http://download.tizen.online/smart-surveillance' + req.url);
\r
54 console.log('33333');
\r
59 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
\r
61 var ENABLE_WEBSOCKET = true;
\r
63 if (ENABLE_WEBSOCKET) {
\r
65 var websocket = require('websocket');
\r
71 var server = new websocket.Server(options, Listener);
\r
73 function Listener(ws) {
\r
74 console.log('Client connected: handshake done!');
\r
76 ws.on('message', function (msg) {
\r
77 console.log('Message received: %s', msg.toString());
\r
78 // ws.send(msg.toString(), {mask: true, binary: false}); //echo
\r
79 // ws.send('Received: ' + msg.toString()); //echo
\r
83 ws.on('ping', function (msg) {
\r
84 console.log('Ping received: %s', msg.toString());
\r
86 ws.on('error', function (msg) {
\r
87 console.log('Error: %s', msg.toString());
\r
92 var timeout = setInterval(function() {
\r
95 var now = Date.now();
\r
98 data = fs.readFileSync('/tmp/latest.jpg');
\r
100 data = fs.readFileSync('/opt/home/dashboard/default.gif');
\r
102 ws.send(data, {mask: false, binary: true});
\r
103 console.log(`Sending frame(${i++}), interval(${now - prev} ms)`);
\r
104 // server.broadcast(data, {mask: false, binary: true});
\r
105 // server.broadcast(`HELLO TO ALL FROM IoT.js!!! (${i++}, ${now - prev})`);
\r
110 ws.on('close', function (msg) {
\r
111 console.log('Client close: ' + msg.reason + ' (' + msg.code + ')');
\r
112 clearInterval(timeout);
\r
116 server.on('error', function (msg) {
\r
117 console.log('Error: %s', msg.toString());
\r
120 server.on('close', function (msg) {
\r
121 console.log('Server close: ' + msg.reason + ' (' + msg.code + ')');
\r