Skip to content

Commit 1cb1c47

Browse files
committed
update: 更优雅的web演示
1 parent 9db1867 commit 1cb1c47

File tree

4 files changed

+185
-123
lines changed

4 files changed

+185
-123
lines changed

example/web/gift.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"192": "赞(系统)",
3+
"193": "弱鸡(系统)",
4+
"519": "呵呵(系统)",
5+
"520": "稳(系统)",
6+
"712": "棒棒哒(系统)",
7+
"714": "怂(系统)",
8+
"824": "荧光棒",
9+
"1859": "小飞碟",
10+
"20000": "鱼丸",
11+
"20001": "弱鸡",
12+
"20002": "办卡",
13+
"20003": "飞机",
14+
"20004": "火箭",
15+
"20005": "超级火箭",
16+
"20006": "",
17+
"20008": "超大丸星",
18+
"20234": "爱心飞机",
19+
"20387": "心动火箭",
20+
"20417": "福袋",
21+
"20541": "大气",
22+
"20542": "666",
23+
"20618": "魔法戒指",
24+
"20624": "魔法皇冠",
25+
"20626": "幸福券",
26+
"20642": "能量电池",
27+
"20643": "能量水晶",
28+
"20644": "能量戒指",
29+
"20709": "壁咚",
30+
"20710": "金鲨鱼",
31+
"20725": "宠爱卡",
32+
"20726": "挚爱超火",
33+
"20727": "乖乖戴口罩",
34+
"20728": "勤洗手",
35+
"20758": "打弱鸡",
36+
"20760": "风暴超火",
37+
"20761": "风暴火箭",
38+
"20885": "告白书",
39+
"20886": "告白卡",
40+
"20887": "告白飞机",
41+
"20888": "告白火箭",
42+
"20889": "告白超火",
43+
"20900": "小厨娘",
44+
"20914": "开黑券",
45+
"20940": "狂欢卡",
46+
"20941": "狂欢火箭",
47+
"20951": "狂欢趴",
48+
"20952": "狂欢飞机",
49+
"20953": "狂欢超火"
50+
}

example/web/host.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"url":"https://a-sh-cn.machine.flxxyz.com/gift.php"}

example/web/index.html

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,144 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>演示页面</title>
8+
<script src="https://flxxyz.github.io/douyudm/dist/douyudanmaku.min.js"></script>
9+
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
10+
<style>
11+
.messageList {
12+
text-decoration: none;
13+
margin: 0;
14+
padding: 0;
15+
overflow-y: scroll;
16+
}
17+
</style>
818
</head>
919

1020
<body>
11-
<div>
12-
<input id="roomId" type="number" placeholder="请输入房间号" value="">
13-
<button id="s">确定</button>
14-
<span id="tips"></span>
21+
<div id='app'>
22+
<div>
23+
<input type='number' placeholder='请输入房间号' v-model='roomId'>
24+
<button @click='enter'>确定</button>
25+
<span>{{ tips }}</span>
26+
</div>
27+
<hr>
28+
<ul class='messageList' :style='{height: messageListHeight}'>
29+
<message v-for='message in messageList' :key='message.id' :message='message' />
30+
</ul>
1531
</div>
16-
<hr>
17-
<ul id="messages" style="text-decoration:none;margin:0;padding:0;overflow-y:scroll;"></ul>
18-
<script src="https://flxxyz.github.io/douyudm/dist/douyudanmaku.min.js"></script>
19-
<script src="./index.js"></script>
32+
33+
<script>
34+
Vue.component('message', {
35+
template: '<li :id="className">{{ message.content }}</li>',
36+
props: ['message'],
37+
computed: {
38+
className() {
39+
return `message-${this.message.id}`
40+
}
41+
},
42+
mounted() {
43+
document.querySelectorAll('li')[document.querySelectorAll('li').length - 1].scrollIntoView()
44+
}
45+
})
46+
47+
var app = new Vue({
48+
el: '#app',
49+
data() {
50+
return {
51+
roomId: '231059',
52+
tips: '',
53+
messageList: [],
54+
messageListHeight: 'auto',
55+
room: null,
56+
closed: null,
57+
isClosed: true,
58+
gift: {},
59+
}
60+
},
61+
methods: {
62+
enter: function () {
63+
var txt = ''
64+
if (this.roomId && this.roomId > 0) {
65+
console.log('enter', this.roomId)
66+
67+
//等待链接关闭再连接
68+
if (this.closed) {
69+
if (this.room) {
70+
this.room.logout()
71+
}
72+
return;
73+
}
74+
75+
if (this.isClosed) {
76+
this.start(this.roomId)
77+
}
78+
79+
this.closed = setInterval(() => {
80+
if (this.room.ws.socket.readyState === 3) {
81+
this.start(this.roomId)
82+
}
83+
}, 5)
84+
} else {
85+
txt = '请输入正确的房间号'
86+
}
87+
this.tips = txt
88+
},
89+
start: function (id) {
90+
this.isClosed = true
91+
this.messageList = []
92+
var echo = this.echo
93+
this.room = new danmaku(id)
94+
this.room.on('connect', () => {
95+
echo(`[connect] roomId=${id}`)
96+
})
97+
this.room.on('disconnect', () => {
98+
echo(`[disconnect] roomId=${id}`)
99+
})
100+
this.room.on('error', err => {
101+
echo(`[error] roomId=${id} ${err}`)
102+
})
103+
this.room.on('uenter', function (res) {
104+
echo(`[uenter] ${res.nn}进入房间`)
105+
})
106+
this.room.on('chatmsg', function (res) {
107+
echo(`[chatmsg] <lv ${res.level}> [${res.nn}] ${res.txt}`)
108+
})
109+
this.room.on('dgb', res => {
110+
var gf = this.gift[res.gfid] || '未知礼物'
111+
echo(`[dgb] 感谢${res.nn}送出${gf} 礼物id=${res.gfid}`)
112+
})
113+
this.room.run()
114+
this.isClosed = false
115+
},
116+
resize: function () {
117+
console.log('onresize')
118+
this.messageListHeight = (document.documentElement.clientHeight - 60) + 'px'
119+
var li = document.querySelectorAll('li')
120+
var liLast = li[li.length - 1]
121+
if (typeof liLast !== 'undefined') {
122+
liLast.scrollIntoView()
123+
}
124+
},
125+
echo: function (message) {
126+
console.log(message)
127+
this.messageList.push({
128+
id: this.uuid(),
129+
content: message,
130+
})
131+
},
132+
uuid: function () {
133+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
134+
let r = Math.random() * 16 | 0,
135+
v = c == 'x' ? r : r & 0x3 | 0x8;
136+
return v.toString(16);
137+
});
138+
}
139+
},
140+
mounted() {
141+
this.resize()
142+
window.onresize = this.resize
143+
},
144+
})
145+
</script>
20146
</body>
21147

22148
</html>

example/web/index.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)