1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3001, path: '/api/v1/pk/websocket' });
let data = { event: 'MATCHSUCCESS', p1: { id: 1, userName: 'outlier-p1', realName: '默认用户', age: 18, email: 'example@xx.com', password: '', schoolId: 1, schoolNumber: '20402131001', avatar: 'https://q9.itc.cn/q_70/images03/20240110/1f984d01360541d3ad28b4d2c0166b1d.jpeg', description: '这个人很懒,什么都没有写噢~', isDeleted: false, version: 1, rating: 0, gender: 1 }, p2: { id: 1, userName: 'outlier-p2', realName: '默认用户', age: 18, email: 'example@xx.com', password: '', schoolId: 1, schoolNumber: '20402131000', avatar: 'https://q9.itc.cn/q_70/images03/20240110/1f984d01360541d3ad28b4d2c0166b1d.jpeg', description: '这个人很懒,什么都没有写噢~', isDeleted: false, version: 1, rating: 0, gender: 1 }, problem: { id: 0, pid: 'OSP0001', title: '小鱼比可爱', background: 'Outlier在开发时复制粘贴的一道测试题', description: '人比人,气死人;鱼比鱼,难死鱼。小鱼最近参加了一个“比可爱”比赛,比的是每只鱼的可爱程度。' + '\n 参赛的鱼被从左到右排成一排,头都朝向左边,然后每只鱼会得到一个整数数值,表示这只鱼的可爱程度,很显然整数越大,表示这只鱼越可爱,而且任意两只鱼的可爱程度**可能一样**。由于所有的鱼头都朝向左边,所以每只鱼只能看见在它左边的鱼的可爱程度,它们心里都在计算,在自己的眼力范围内有多少只鱼不如自己可爱呢。请你帮这些可爱但是鱼脑不够用的小鱼们计算一下。', inputFormat: '第一行输入一个正整数 $n$,表示鱼的数目。\n' + '第二行内输入 $n$ 个正整数,用空格间隔,依次表示从左到右每只小鱼的可爱程度 $a_i$。\n', outputFormat: '一行,输出 $n$ 个整数,用空格间隔,依次表示每只小鱼眼中有多少只鱼不如自己可爱。', hint: '对于 $100%$ 的数据,$1 \\leq n\\leq 100$,$0 \\leq a_i \\leq 10$。', difficulty: 100, authorId: 0, createTime: Date.now(), timeLimit: 1, memoryLimit: 128, isPublic: true, isSpj: false, isAudited: false, isDeleted: false, version: 0 }, sampleList: [ [{ input: '3\n1 2 3', output: '6' }, { input: '3\n1 2 3', output: '6' }] ] }
wss.on('connection', function (ws) { console.log(`${new Date} A new WebScoket Connected!`);
ws.on('message', function (message) { console.log('received: %s', message); let msg = JSON.parse(message); if(msg['event'] === 'STOPMATCH') ws.close(); });
ws.on('close', function () { console.log('The client has disconnected.'); });
ws.on('error', function (error) { console.error('WebSocket error:', error); });
setInterval(() => { ws.send(JSON.stringify(data)) }, 5000) });
console.log('WebSocket server is running on ws://localhost:3001/api/v1/pk/websocket');
|