Last active
December 13, 2015 17:28
-
-
Save pinkerton/4947821 to your computer and use it in GitHub Desktop.
Read data sent over TCP from the Python script to the browser via socket.io on the Node server. Problems occur when I open my browser to localhost:8081 and the terminal fills with a bunch of random numbers and the page loads incredibly slow, and the browser doesn't seem to receive any data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
min_graph = 0; | |
max_graph = 0; | |
options = { | |
series: { shadowSize: 0 }, // drawing is faster without shadows | |
xaxis: { show: false }, | |
legend: { show: true } | |
}; | |
$(function () { | |
var lowV = 1000; | |
var lowS = 1000; | |
var maxPoints = 5 * 50; | |
var dataSetpoint = []; | |
var dataValue = []; | |
var dataControl = []; | |
lowest = 1000000; | |
highest = 0; | |
function getData() { | |
var resS = []; | |
var resV = []; | |
var resC = []; | |
var min = 0; | |
var max = 0; | |
for (var i = 0; i < maxPoints; ++i) | |
if( i > dataSetpoint.length) | |
resS.push([i, 0]); | |
else { | |
resS.push([i, dataSetpoint[i]]); | |
min = (dataSetpoint[i] < min) ? dataSetpoint[i] : min; | |
max = (dataSetpoint[i] > max) ? dataSetpoint[i] : max; | |
} | |
for (var i = 0; i < maxPoints; ++i) | |
if( i > dataValue.length) | |
resV.push([i, 0]); | |
else { | |
resV.push([i, dataValue[i]]); | |
min = (dataValue[i] < min) ? dataValue[i] : min; | |
max = (dataValue[i] > max) ? dataValue[i] : max; | |
} | |
for (var i = 0; i < maxPoints; ++i) | |
if( i > dataControl.length) | |
resC.push([i, 0]); | |
else { | |
resC.push([i, dataControl[i]]); | |
min = (dataControl[i] < min) ? dataControl[i] : min; | |
max = (dataControl[i] > max) ? dataControl[i] : max; | |
} | |
min_graph = min; | |
max_graph = max; | |
var res = [{label: "S", data: resS}, | |
{label: "V", data: resV}, | |
{label: "C", data: resC}]; //console.log(res); | |
return res; | |
} | |
// setup plot | |
function makeplot() { | |
$('#pid_plot').css('width', function(index) { | |
return $(window).width() * .9; | |
}); | |
$('#pid_plot').css('height', function(index) { | |
return $(window).height() * .8; | |
}); | |
plot = $.plot($("#pid_plot"), getData(), options); | |
} | |
makeplot(); | |
$(window).resize(makeplot); | |
function push(newS, newV, newC) { | |
var min; | |
if (dataSetpoint.length > 0) | |
dataSetpoint = dataSetpoint.slice(1); | |
if (newS < lowest) { | |
lowest = newS; | |
} | |
if (newS > highest) { | |
highest = newS; | |
} | |
while (dataSetpoint.length < maxPoints) { | |
var y = newS; | |
dataSetpoint.push(y); | |
} | |
if (dataValue.length > 0) | |
dataValue = dataValue.slice(1); | |
while (dataValue.length < maxPoints) { | |
var y = newV; | |
dataValue.push(y); | |
} | |
if (dataControl.length > 0) | |
dataControl = dataControl.slice(1); | |
while (dataControl.length < maxPoints) { | |
//var y = (newC * ((max_graph-5 - (min_graph+5))/2)) + ((max_graph-5 - (min_graph+5))/2) ; | |
var y = newC; | |
dataControl.push(y); | |
} | |
plot.setupGrid(); | |
plot.setData(getData()); | |
plot.draw(); | |
} | |
var socket = io.connect('http://localhost'); | |
socket.on('update', function (dataIn) { | |
push(dataIn["S"], dataIn["V"],dataIn["C"]); | |
console.log(dataIn); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var net = require("net"); | |
var app = require("express"); | |
var server = require('http').createServer(app).listen(8081); | |
var io = require('socket.io').listen(server); | |
var HTTP_PORT = 8081; | |
var TCP_PORT = 41234; | |
//app.listen(HTTP_PORT); | |
express.use("/js", express.static(__dirname + '/js')); | |
express.use("/css", express.static(__dirname + '/css')); | |
express.use("/images", express.static(__dirname + '/images')); | |
express.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
var clients = []; | |
net.createServer(function (socket) { | |
// Identify the client | |
socket.name = socket.remoteAddress + ":" + socket.remotePort | |
// Put this new client in the list | |
clients.push(socket); | |
broadcast(socket.name + " connected", socket); | |
// Handle incoming messages from clients. | |
socket.on('data', function (data) { | |
broadcast(data, socket); | |
}); | |
// Remove the client from the list when it leaves | |
socket.on('end', function () { | |
clients.splice(clients.indexOf(socket), 1); | |
//broadcast(socket.name + " disconnected."); | |
}); | |
// Send a message to all clients | |
function broadcast(message, sender) { | |
io.sockets.emit('update', message); | |
console.log(message.toString("utf8")); | |
} | |
}).listen(TCP_PORT); | |
// Put a friendly message on the terminal of the server. | |
console.log("Listening..."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import math | |
import socket | |
TCP_IP = "127.0.0.1" | |
TCP_PORT = 41234 | |
SLEEP_INTERVAL = 1 | |
HOST, PORT = TCP_IP, TCP_PORT | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((HOST, PORT)) | |
i = 0 | |
while True: | |
try: | |
while True: | |
msg='{"S":%d, "V":%d, "C":%d}' % (((math.sin(i) + 1) *50), ((math.cos(i) + 1) * 50), (math.cos(i))) | |
sock.sendall(msg + "|") | |
print "Sent:\t{}".format(msg) | |
time.sleep(SLEEP_INTERVAL) | |
i += .1 | |
finally: | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment