Skip to content

Commit

Permalink
Merge pull request #2 from UestcCarpediem/dev_backend
Browse files Browse the repository at this point in the history
Dev backend
  • Loading branch information
Kingfish404 authored Feb 23, 2021
2 parents 0e78c7b + 02dd15d commit 1e4a9d9
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 69 deletions.
29 changes: 28 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ response:
author: 'w3.org',
url: 'https://media.w3.org/2020/08/ml-workshop/virtual-character-web-meeting.mp4',
desc: 'virtual-character-web-meeting.mp4',
musice: 'popmusic-name',
tags: [
'mc', 'course',
],
Expand Down Expand Up @@ -55,7 +56,33 @@ response:
## Socket.io

```javascript
url: /ws
// url后的query,id为当前视频id,下方所有的id,均为当前视频id
url: /ws?id=Number(id)

// id: id,对应评论的房间号

desc: 切换视频/live
send: // id:新room id,id_old:旧id,用于退出之前的聊天空间
socket.emit('rejoin', { id: Number(id), id_old: Number(id_old) });
res:
socket.emit('getrejoin', { id: Number(id), comments: {} });

desc: 点赞
send:
socket.emit('setlike', { id: Number(id)});
res:
socket.emit('getlike', { id: Number(id), like: Number(like) });

desc: 发评论
send:
socket.emit('setcomment', { id: Number(id), data: 'comment', name: 'userName' });
res:
none

desc: 收评论
res:
socket.emit('getcomment', { id: 0, data: 'comment' , name: 'userName' });

```

## 报错
Expand Down
45 changes: 39 additions & 6 deletions server/app/io/controller/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,50 @@

module.exports = app => {
class Controller extends app.Controller {
async rejoin() {
async rejoin(socket) {
// 切换加入房间
const { ctx } = this;
const message = ctx.args[0];
console.log(ctx.args);
await ctx.socket.emit('res', `Hi! I've got your message: ${message}`);
if (message) {
const id = message.id;
const id_old = message.id_old;
console.log('rejoin', message);
if (id !== null) {
const roomId = 'room-' + String(id);
const roomId_old = 'room-' + String(id_old);
// console.log('#rejoin#room:' + roomId);
socket.join(roomId);
socket.leave(roomId_old);
await ctx.socket.emit('getrejoin', { id: Number(id), comments: {}, get: message });
}
}
}
async chat() {
async like() {
// 喜欢
const { ctx } = this;
const message = ctx.args[0];
console.log(ctx.args);
await ctx.socket.emit('res', `Hi! I've got your message: ${message}`);
console.log('like:', message);
const id = message.id;
await ctx.socket.emit('getlike', { id: Number(id), like: 20000, get: message });
}
async comment() {
// 发布评论
const { ctx } = this;
const message = ctx.args[0];
const comment = message.data;
console.log('comment', message);
if (message) {
if (message.id !== null) {
const id = message.id;
const roomId = 'room-' + String(id);
const name = message.name;
// console.log('#comment#room:' + roomId);
await ctx.socket.to(roomId).emit('getcomment', { id: Number(id), data: comment, name: String(name) });
}
} else {
console.log('get msg failed!');
await ctx.socket.emit('getcomment', { id: 0, data: 'error!id is empty!', get: message });
}
}
}
return Controller;
Expand Down
12 changes: 10 additions & 2 deletions server/app/io/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
// eslint-disable-next-line no-unused-vars
module.exports = app => {
return async (ctx, next) => {
const { socket } = ctx;
// console.log('auth:', ctx.query);
ctx.socket.emit('res', 'connected!');
await next();
const query = ctx.query;
const id = query.id;
if (id !== null) {
const roomId = 'room-' + String(id);
console.log('#room:' + roomId);
socket.join(roomId);
}
ctx.socket.to('room-' + String(id)).emit('res', 'connected!');
await next(socket);
// execute when disconnect.
console.log('disconnection!');
};
Expand Down
5 changes: 4 additions & 1 deletion server/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ module.exports = app => {
router.get('/api/live/getlist', controller.live.getLiveList);

// websocket.io
io.set('origins', '*:*');
io.of('/ws').route('rejoin', io.controller.default.rejoin);

io.of('/ws').route('chat', io.controller.default.chat);
io.of('/ws').route('setlike', io.controller.default.like);

io.of('/ws').route('setcomment', io.controller.default.comment);

// 错误url处理
router.get(/^.*$/i, controller.error.pageNotFound);
Expand Down
34 changes: 33 additions & 1 deletion server/app/service/live.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const Service = require('egg').Service;
class LiveService extends Service {
// 从数据库获取视频列表
async getLiveList(vid = 0) {
const vdata = [{
// eslint-disable-next-line no-unused-vars
const vdata_back = [{
id: vid,
author: 'cctv',
url: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
Expand All @@ -26,6 +27,37 @@ class LiveService extends Service {
comment: 0,
share: 0,
}];
const vdata = [{
id: 100001,
author: '西瓜测试直播1',
url: 'http://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8',
desc: 'XGvideo1',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z',
like: 0,
comment: 0,
share: 0,
}, {
id: 100002,
author: '西瓜测试直播2',
url: 'http://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8',
desc: 'XGvideo2',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z',
like: 0,
comment: 0,
share: 0,
}, {
id: 100003,
author: 'CCTV1',
url: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
desc: 'cctv1',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z',
like: 0,
comment: 0,
share: 0,
}];
return vdata;
}
}
Expand Down
8 changes: 5 additions & 3 deletions server/app/service/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class VideoService extends Service {
id: vid,
author: 'w3.org',
url: 'https://media.w3.org/2020/08/ml-workshop/virtual-character-web-meeting.mp4',
desc: 'virtual-character-web-meeting.mp4',
desc: 'virtual-character-web-meeting',
musice: 'popmusic-name',
tags: [
'mc', 'course',
],
Expand All @@ -19,8 +20,9 @@ class VideoService extends Service {
}, {
id: vid + 1,
author: 'w3.org',
url: 'https://media.w3.org/2020/08/ml-workshop/virtual-character-web-meeting.mp4',
desc: 'virtual-character-web-meeting.mp4',
url: 'https://media.w3.org/2020/08/ml-workshop/MEYER_RAUCHENSTEIN_artie_video.mp4',
desc: 'MEYER_RAUCHENSTEIN_artie_video',
musice: 'popmusic-name',
tags: [
'mc', 'course',
],
Expand Down
5 changes: 5 additions & 0 deletions server/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ module.exports = appInfo => {
// myAppName: 'egg',
};

config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
};

// 导入socket.io
config.io = {
init: {}, // passed to engine.io
Expand Down
55 changes: 0 additions & 55 deletions server/test/socket/index.html

This file was deleted.

103 changes: 103 additions & 0 deletions server/test/socket/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Socket test</title>
</head>

<body>
<h1>Socket.io Testing</h1>
<div>
<p>Current Room:<span id="room"></span></p>
</div>
<ul id="events"></ul>
<div>
<label>
room
<input id='inputRoom' value="0">
</label>
</div>
<div>
<label>
msg
<input id='input'>
</label>
</div>

<button id="btn" onclick="sendMsgStar()">SendLike</button>
<button id="btn" onclick="sendMsgComment()">SendComment</button>
<button id="btn" onclick="sendMsgReJoin()">SendReJoin</button>

<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2/dist/socket.io.js"></script>
<script>
const $event = document.getElementById('events');

const $input = document.getElementById('input');
const $inputRoom = document.getElementById('inputRoom');
const $room = document.getElementById('room');
const state = { id: 0 };
$room.innerText = state.id;

const newItem = (content) => {
const item = document.createElement('li');
if (typeof content == 'object') {
item.innerHTML = JSON.stringify(content);
} else {
item.innerHTML = content;
}
return item;
}

function startConnect() {
const socket = io('ws://127.0.0.1:7001/ws?id=0');
// const socket = io('ws://tiktok.kingfish404.cn/ws?id=0');

socket.on('connect', () => {
$event.appendChild(newItem('connect'));
socket.send('123123');
});

socket.on('getrejoin', (res) => {
$event.appendChild(newItem(res));
});

socket.on('getlike', (res) => {
$event.appendChild(newItem(res));
});

socket.on('getcomment', (res) => {
$event.appendChild(newItem(res));
});

socket.on('comment')

window.socket = socket;
}

function sendMsgStar() {
const msg = $input.value;
window.socket.emit('setlike', { id: Number(state.id) });
}

function sendMsgComment() {
const msg = $input.value;
window.socket.emit('setcomment', { id: Number(state.id), data: msg, name: 'userName' });
}

function sendMsgReJoin() {
const msg = $input.value;
const roomId = $inputRoom.value;
const roomIdOld = state.id;
state.id = roomId;
$room.innerText = roomId;
window.socket.emit('rejoin', { id: Number(roomId), id_old: Number(roomIdOld) });
}

startConnect();
</script>
</body>

</html>

0 comments on commit 1e4a9d9

Please sign in to comment.