You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.1 KiB
Plaintext

7 months ago
var http = require('http');
var php = require('php');
var fs = require('fs');
var phpExpress = require('php-express')({
binPath: 'php'
});
var express = require('express');
var app = express();
var path = require('path');
var server = http.createServer(app);
//var server = http.createServer(php);
//var port = 8000;
var osc = require('osc');
var activeUsers = new Set(); // Active users Set
/////////////////////////////////////////////////////// php
var router = express.Router();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 8000));
//app.get('/',function(req,res){
// res.sendFile(path.join(__dirname, 'public', 'index.php'));
//});
app.use('/', express.static(__dirname));
app.set('public', path.join(__dirname, '/public'));
app.engine('php', phpExpress.engine);
app.set('view engine', 'php');
app.all(/.+\.php$/, phpExpress.router);
app.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!")
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
//server.listen(port, () => {
// console.log("Server is listening at port %d", port);
//});
//app.use(express.static(path.join(__dirname, "public")));
//app.use(express.static('public'));
//var server = app.listen(8000, function() {
// console.log('listening on requests on port 8000');
//});
//
//app.get('/',function(req,res){
// res.sendFile(path.join(__dirname, 'public', 'index.php'));
//});
//app.set('views', __dirname+'public');
//app.engine('php', phpExpress.engine);
//app.all('/index.php', function(req, res) {
// res.render('phpinfo');
//})
//app.all(/.+\.php$/, phpExpress.router);
//Static files
//app.use(express.static('public'));
////////////////////////////////////////////////////////////
var udpPort = new osc.UDPPort({
// This is the port we're listening on.
localAddress: "0.0.0.0",
localPort: 57121,
// This is where sclang is listening for OSC messages.
remoteAddress: "192.168.2.15", //this is the hamsters ip
remotePort: 8080, //this is the hamsters port :*s
metadata: true
});
// Open the socket.
udpPort.open();
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 })
const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' }))
//server.listen(port, () => {
// console.log("Server is listening at port %d", port);
//});
//app.use(express.static(path.join(__dirname, "public")));
//app.use(express.static('public'));
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log("new user online!");
// Add the new socket ID to the activeUsers Set
activeUsers.add(socket.id);
// Emit the updated activeUsers Set to all connected clients
io.emit('activeUsers', Array.from(activeUsers));
parser.on('data', function(data) {
const msg = data.split(' ');
console.log(msg[0], msg[1]);
io.emit('node-data', data);
});
socket.on("blink", () => {
console.log("Blink event received!");
io.emit("blink"); // Emit the "blink" event to all connected clients
});
socket.on('chat message', (msg) => {
console.log('[user][' + socket.id + '][' + msg + ']');
socket.broadcast.emit('chat message', msg);
var oscmsg = {
address: "/silentserver",
args: [
{
type: "s",
value: socket.id
},
{
type: "s",
value: msg
}
]
};
udpPort.send(oscmsg);
});
socket.on('userposition', (msg) => {
console.log('[user][' + socket.id + '][position: ' + msg[0] + ',' + msg[1] + ']');
socket.to('expo').emit(socket.id, msg);
});
// Handle disconnection event
socket.on('disconnect', () => {
console.log("new user online:", socket.id);
// Remove the disconnected socket ID from the activeUsers Set
activeUsers.delete(socket.id);
// Emit the updated activeUsers Set to all connected clients
io.emit('activeUsers', Array.from(activeUsers));
});
});