koa2与Express物联网搭建示例

koa2物联网搭建示例

const PORT = 9091;

const fs = require("fs");
const http = require("http");
const path = require("path");

// IOT服务
const iot = require("./iot/runtime");
const iotInstance = new iot(require("./config").runMode, 2000); //production or development
iotInstance.run();

// koa2
const Koa = require("koa");
const app = new Koa();

// 静态资源加载
const Static = require("koa-static");
app.use(Static(path.join(__dirname, "../web/client-dist")));

// 加载html、ejs模板页面
const Views = require("koa-views");
app.use(Views(path.join(__dirname, "../web/client-dist")));
// app.use(Views(path.join(__dirname, "../web/client-dist"), {
//   extension: 'ejs'
// }))


// websocket
const server = http.createServer(app.callback());
const Socket = require("socket.io");
const io = Socket(server);
const sockets: any[] = [];
io.on("connect", (socket: { id: any }) => {
  console.log("socket conncet,id is:", socket.id);
  sockets.push(socket);
});
io.on("disconnect", () => {
  console.log("disconnect");
});
setInterval(() => {
  io.emit("msg", iotInstance.Model);
  console.log(
    sockets.map((item) => {
      return {
        id: item.id,
        connect_status: item.connected,
      };
    })
  );
}, 1000);

//http lisenter
server.listen(PORT, function () {
  console.log("server is running on http://127.0.0.1:9091");
});

Express物联网搭建示例

const http = require("http");
const express = require("express");
const Socket = require("socket.io");
const path = require("path");
const iot = require("./iot/runtime");
const { json } = require("express");

const iotInstance = new iot(require("./config").runMode, 2000); //production or development
iotInstance.run();

const app = express();
app.use("/", express.static(path.join(__dirname, "../client-dist")));
app.get("/", (req: any, res: { sendFile: (arg0: any) => void }) => {
  res.sendFile(path.join(__dirname, "../client-dist/index.html"));
});

const server = http.createServer(app);

//websocket
const io = Socket(server);
const sockets: any[] = [];
io.on("connect", (socket: { id: any }) => {
  console.log("socket conncet,id is:", socket.id);
  sockets.push(socket);
});
io.on("disconnect", () => {
  console.log("disconnect");
});
setInterval(() => {
  io.emit("msg", iotInstance.Model);
  console.log(
    sockets.map((item) => {
      return {
        id: item.id,
        connect_status: item.connected,
      };
    })
  );
}, 1000);

//http lisenter
server.listen(9091, function () {
  console.log("server is running on http://127.0.0.1:9091");
});

你可能感兴趣的:(javascript,javascript,前端,开发语言)