forked from SeanLeeCai/onlineChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudience.html
More file actions
executable file
·179 lines (170 loc) · 4.69 KB
/
audience.html
File metadata and controls
executable file
·179 lines (170 loc) · 4.69 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/main.css">
<title>SkyRTC聊天室Demo</title>
<style type="text/css">
@media screen and (min-width: 500px) and (max-width: 700px) {
body,html {
height: 100%;
margin:0;
}
#videos video {
width: auto;
height: 100%;
}
#chat {
left: 75%;
}
#msgIpt {
width: 52% !important;
}
#sendBtn {
width: 30% ;
}
}
@media screen and (min-width: 400px) and (max-width: 500px) {
body,html {
height: 100%;
margin:0;
}
#videos video {
width: auto;
height: 100%;
}
#chat {
display: none;
}
}
@media screen and (max-width: 400px) {
body,html {
margin: 0;
padding: 0;
}
#videos video {
width: 100%;
}
#chat {
display: none;
}
#videos {
position: static;
width: 100%;
}
}
@media screen and (min-width:700px) {
video {
height: 100%;
}
}
</style>
</head>
<body>
<div id="chat" onkeydown="submitByEnter();">
<div class="msgs" id="msgs"></div>
<div id="sendMsg">
<input type="text" id="msgIpt" class="msgIpt">
<button id="sendBtn" class="sendBtn">发送</button>
</div>
</div>
<div id="videos">
</div>
<div id="files">
</div>
</body>
<script type="text/javascript" src="/RTC-audience.js"></script>
<script type="text/javascript">
var videos = document.getElementById("videos");
var sendBtn = document.getElementById("sendBtn");
var msgs = document.getElementById("msgs");
var rtc = SkyRTC();
/**********************************************************/
function submitByEnter(){
var event = window.event || arguments.callee.caller.arguments[0];
if(event.keyCode == 13) {
var msgIpt = document.getElementById("msgIpt"),
msg = msgIpt.value,
p = document.createElement("p");
if (!msg) {
alert("亲,说点什么吧!");
return;
};
p.innerHTML = "me: " + msg;
msgs.appendChild(p);
//广播消息
if(document.getElementById("chat").scrollHeight>document.body.clientHeight){
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
}
rtc.broadcast(msg);
msgIpt.value = "";
};
}
sendBtn.onclick = function(event){
var msgIpt = document.getElementById("msgIpt"),
msg = msgIpt.value,
p = document.createElement("p");
if (!msg) {
alert("亲,说点什么吧!");
return;
};
p.innerHTML = "me: " + msg;
//广播消息
if(document.getElementById("chat").scrollHeight>document.body.clientHeight){
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
}
rtc.broadcast(msg);
msgIpt.value = "";
msgs.appendChild(p);
};
//成功创建WebSocket连接
rtc.on("connected", function(socket) {
//创建本地视频流
rtc.createStream({
"video": true,
"audio": true
});
});
//创建本地视频流成功
rtc.on("stream_created", function(stream) {
//document.getElementById('me').src = URL.createObjectURL(stream);
//document.getElementById('me').play();
});
//创建本地视频流失败
rtc.on("stream_create_error", function() {
//alert("create stream failed!");
});
//接收到其他用户的视频流
rtc.on('pc_add_stream', function(stream, socketId) {
var newVideo = document.createElement("video"),
id = "other-" + socketId;
newVideo.setAttribute("class", "other");
newVideo.setAttribute("autoplay", "autoplay");
newVideo.setAttribute("id", id);
videos.appendChild(newVideo);
var v = document.getElementById(id);
rtc.attachStream(stream, id);
});
//删除其他用户
rtc.on('remove_peer', function(socketId) {
var video = document.getElementById('other-' + socketId);
if(video){
video.parentNode.removeChild(video);
}
});
//接收到文字信息
rtc.on('data_channel_message', function(channel, socketId, message){
var p = document.createElement("p");
p.innerHTML = message;
var t='#';
for(j=0;j<6;j++) {
t = t+ socketId.charAt(j);
}
p.setAttribute("style","color:"+t);
msgs.appendChild(p);
});
//连接WebSocket服务器
rtc.connect("ws:" + window.location.href.substring(window.location.protocol.length).split('#')[0], window.location.hash.slice(1));
</script>
</html>