Skip to content

Commit

Permalink
update:finish socket api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingfish404 committed Feb 23, 2021
1 parent 898df30 commit 02dd15d
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 32 deletions.
17 changes: 10 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,26 @@ response:
## Socket.io

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

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

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

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

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

Expand Down
47 changes: 37 additions & 10 deletions server/app/io/controller/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,50 @@

module.exports = app => {
class Controller extends app.Controller {
async rejoin() {
async rejoin(socket) {
// 切换加入房间
const { ctx } = this;
const message = ctx.args;
console.log('rejoin', ctx.args);
await ctx.socket.emit('getrejoin', { id: 1, comments: {}, get: message });
const message = ctx.args[0];
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 like() {
// 喜欢
const { ctx } = this;
const message = ctx.args;
console.log('like:', ctx.args);
await ctx.socket.emit('getlike', { id: 0, like: 20000, get: message });
const message = ctx.args[0];
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;
console.log('comment', ctx.args);
await ctx.socket.emit('getcomment', { id: 0, data: 'comment', name: 'userName' });
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
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
6 changes: 3 additions & 3 deletions server/app/service/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 @@ -20,8 +20,8 @@ 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
39 changes: 30 additions & 9 deletions server/test/socket/index.html → server/test/socket/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@

<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>

<input id='input'>
<button id="btn" onclick="sendMsgStar()">SendLike</button>
<button id="btn" onclick="sendMsgComment()">SendComment</button>
<button id="btn" onclick="sendMsgReJoin()">SendReJoin</button>
Expand All @@ -22,21 +36,24 @@ <h1>Socket.io Testing</h1>
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');
console.log(typeof(content));
if(typeof content == 'object'){
if (typeof content == 'object') {
item.innerHTML = JSON.stringify(content);
}else{
} else {
item.innerHTML = content;
}
return item;
}

function startConnect() {
const socket = io('ws://127.0.0.1:7001/ws?token=123123');
// const socket = io('ws://tiktok.kingfish404.cn/ws?token=123123');
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'));
Expand All @@ -62,17 +79,21 @@ <h1>Socket.io Testing</h1>

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

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

function sendMsgReJoin() {
const msg = $input.value;
window.socket.emit('rejoin', { id: 1 });
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();
Expand Down

0 comments on commit 02dd15d

Please sign in to comment.