一、服务器端:
基本和nodejs工程相同 https://www.cnblogs.com/xuanmanstein/p/10509445.html
安装socket.io
npm i --save socket.io
工程代码
import express from "express";
import fs from "fs";
//--------读yaml配置文件------------
const yaml = require('js-yaml');
const argv = require('yargs').argv;
console.log('argv', argv);
const config = yaml.safeLoad(fs.readFileSync(argv.config, { encoding: 'utf8', flag: "r" }));
console.log('yaml config', config); const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http); const port = 55555; app.get("/", (req, res) => res.send("Hello World!")); io.on('connection', function(socket){
console.log('a user connected');
socket.broadcast.emit('hi'); socket.on('chat message', function(msg){
io.emit('chat message', msg);
}); socket.on('disconnect', function(){
console.log('user disconnected');
}); }); process.on('uncaughtException', function (e) {
console.log(e);
}); app.listen(port, () => console.log(`socket.io app listening on port ${port}!`));