Open API with clent

This commit is contained in:
2024-03-10 19:13:28 +07:00
parent f1ccdb35bf
commit 4bc42d6ae9
5 changed files with 12 additions and 4 deletions

View File

@@ -1,20 +1,22 @@
import { Socket } from "socket.io";
import { Pin } from "johnny-five";
import { board } from "../setup";
export default (socket: Socket) => {
console.log(`${socket.id} | ${socket.client.request.headers.host} | Joined`);
socket.on("control-servo", (p: string, ang: string) => {
socket.on("servo", (p: string, ang: string) => {
const pin = Number.parseInt(p);
const angle = Number.parseInt(ang);
board.pinMode(pin, Pin.SERVO);
board.servoWrite(pin, angle);
console.log(socket.id, pin, angle);
})
socket.on("read-photoresistor", (pin: string) => {
socket.on("set-photoresistor", (pin: string) => {
board.analogRead(pin, (value) => {
socket.emit("read-photoresistor", value);
socket.emit("photoresistor", value);
console.log(socket.id, "A"+pin, value);
});
})

View File

@@ -1,5 +1,6 @@
import http from 'node:http';
import express from 'express';
import cors from 'cors';
import { Server } from 'socket.io';
import chalk from 'chalk';
@@ -23,6 +24,9 @@ const host: string = 'localhost';
const port: number = 3000;
// Express middleware
app.use(cors({
origin: "*"
}));
app.use(express.json());
app.use(express.static('client'));