add more nodejs instrumentation
This commit is contained in:
parent
e79c419e40
commit
060e0caa44
42
index.html
42
index.html
|
@ -3,13 +3,45 @@
|
|||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
var socket = io();
|
||||
console.log("loaded socketio");
|
||||
// console.log("loaded socketio");
|
||||
|
||||
// connect to socketio
|
||||
socket.on('connect', function(){
|
||||
socket.send('hi');
|
||||
socket.on('data', function(data){
|
||||
console.log(data);
|
||||
});
|
||||
// send connect message
|
||||
socket.send('hi '+window.location);
|
||||
});
|
||||
window.onload = function(){
|
||||
cons = document.getElementById("console");
|
||||
// handle user input
|
||||
cons.onsubmit = function(){
|
||||
inp = document.getElementById("input")
|
||||
scr = document.getElementById("screen")
|
||||
// send input to socketio
|
||||
socket.emit('data',inp.value);
|
||||
// console.log(inp.value);
|
||||
scr.value += "> "+inp.value+"\r";
|
||||
scr.scrollTop = scr.scrollHeight;
|
||||
inp.value = "";
|
||||
return false;
|
||||
};
|
||||
scr = document.getElementById("screen")
|
||||
scr.value = ""; // reset input onload
|
||||
// log data from socket
|
||||
socket.on('data', function(data){
|
||||
// console.log(data);
|
||||
scr.value += "< "+data+"\r";
|
||||
scr.scrollTop = scr.scrollHeight;
|
||||
});
|
||||
// log connect messages
|
||||
socket.on('message', function(data){
|
||||
console.log('message',window.location,data);
|
||||
});
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<form id="console">
|
||||
<textarea cols=80 rows=20 id="screen"></textarea><br/>
|
||||
<input id="input" type="text" />
|
||||
<input id="submit" type="button" value="Submit" />
|
||||
</form>
|
||||
</html>
|
29
serial.js
29
serial.js
|
@ -1,12 +1,12 @@
|
|||
const SerialPort = require('serialport');
|
||||
const Readline = require('@serialport/parser-readline');
|
||||
|
||||
var express = require('express'),
|
||||
app = express(),
|
||||
http = require('http'),
|
||||
server = http.createServer(app),
|
||||
io = require('socket.io').listen(server),
|
||||
scores = {};
|
||||
var express = require('express'),
|
||||
app = express(),
|
||||
http = require('http'),
|
||||
server = http.createServer(app),
|
||||
io = require('socket.io').listen(server),
|
||||
scores = {};
|
||||
|
||||
// listen for new web clients:
|
||||
server.listen(8080);
|
||||
|
@ -14,7 +14,7 @@ server.listen(8080);
|
|||
// open the serial port. Change the name to the name of your port, just like in Processing and Arduino:
|
||||
const port = new SerialPort("/dev/ttyUSB0", { baudRate: 9600 });
|
||||
|
||||
const parser = new Readline({ delimiter: '\r' });
|
||||
const parser = new Readline({ delimiter: '\r\n' });
|
||||
port.pipe(parser)
|
||||
|
||||
// respond to web GET requests with the index.html page:
|
||||
|
@ -27,9 +27,18 @@ parser.on('data', line => console.log(`> ${line}`))
|
|||
|
||||
// listen for new socket.io connections:
|
||||
io.on('connection', function (socket) {
|
||||
parser.on('data', line => socket.emit('data', line))
|
||||
// get data from serial and send it to socket
|
||||
parser.on('data', line => {
|
||||
socket.emit('data', line)
|
||||
});
|
||||
// get data from socket and send it to serial
|
||||
socket.on('data', function (data) {
|
||||
console.log('data', socket.conn.remoteAddress, data)
|
||||
port.write(data+"\r") // we need to end each command with a CR
|
||||
});
|
||||
// send and log connect messages
|
||||
socket.volatile.emit('message', "hi "+socket.conn.remoteAddress);
|
||||
socket.on('message', function (data) {
|
||||
console.log(data)
|
||||
port.write(data+'\r')
|
||||
console.log('message',socket.conn.remoteAddress,data)
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user