-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
111 lines (108 loc) · 2.82 KB
/
main.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
try {
new Vue({
el: '#app',
data: {
sonicSocket: null,
sonicServer: null,
EMOTICONS: ['happy', 'sad', 'heart', 'mad', 'star', 'oh'],
ALPHABET: '',
mode: 'ultrasonic',
howVisualizer: false,
message: '。。。',
customStr: ''
},
created() {
// this.ALPHABET = this.generateAlphabet(this.EMOTICONS);
this.ALPHABET = '0123456789*';
},
mounted() {
this.createSonicNetwork();
},
methods: {
generateAlphabet: function(list) {
var alphabet = '';
for (var i = 0; i < Math.min(list.length, 9); i++) {
alphabet += i.toString();
}
return alphabet;
},
createSonicNetwork: function(opt_coder) {
// Stop the sonic server if it is listening.
// 如果已经在监听就停止
if (this.sonicServer) {
this.sonicServer.stop();
}
if (opt_coder) {
this.sonicServer = new SonicServer({ coder: opt_coder });
this.sonicSocket = new SonicSocket({ coder: opt_coder });
} else {
this.sonicServer = new SonicServer({ alphabet: ALPHABET, debug: false });
this.sonicSocket = new SonicSocket({ alphabet: ALPHABET });
/* this.sonicServer = new SonicServer({ debug: false });
this.sonicSocket = new SonicSocket({}); */
}
this.sonicServer.start();
this.sonicServer.on('message', this.onIncomingEmoticon);
},
toggleMode: function() {
if (this.mode === 'ultrasonic') {
this.mode = 'audible';
var coder = new SonicCoder({
freqMin: 440, // 最低评率
freqMax: 1760
});
this.createSonicNetwork(coder);
} else {
this.mode = 'ultrasonic';
this.createSonicNetwork();
}
},
toggleShowVisualizer: function() {
this.howVisualizer = !this.howVisualizer;
sonicServer.setDebug(this.howVisualizer);
},
handleSendCustomStr: function() {
if (this.customStr) {
let sendStr = this.divideSameLetter(this.customStr);
this.sonicSocket.send(sendStr);
}
},
// 点击 发送声音
handleClickEmo: function(emo) {
console.log(emo);
this.sonicSocket.send(emo.toString());
},
onIncomingEmoticon: function(message) {
console.log('message: ' + message);
this.message = this.deleteDivide(message);
},
// 在相邻的字符间加入 ‘-’
divideSameLetter: function(source) {
if (!source) {
return;
}
const divide = '*';
const letters = source.split('');
let prev;
let target = [divide];
for (let i = 0, len = letters.length; i < len; i++) {
let letter = letters[i];
if (letter === prev) {
target.push(divide);
} else {
prev = letter;
}
target.push(letter);
}
target.push(divide);
return target.join('');
},
// 去除字符里的 ‘-’
deleteDivide: function(source) {
return source.replace(/\*/g, '');
}
}
});
} catch (error) {
alert(error.toString());
}