使用方法
var sendChannel,
receiveChannel,
chatWindow = document.querySelector('.chat-window'),
chatWindowMessage = document.querySelector('.chat-window-message'),
chatThread = document.querySelector('.chat-thread');
// Create WebRTC connection
createConnection();
// On form submit, send message
chatWindow.onsubmit = function (e) {
e.preventDefault();
sendData();
return false;
};
function createConnection () {
var servers = null;
if (window.mozRTCPeerConnection) {
window.localPeerConnection = new mozRTCPeerConnection(servers, {
optional: [{
RtpDataChannels: true
}]
});
} else {
window.localPeerConnection = new webkitRTCPeerConnection(servers, {
optional: [{
RtpDataChannels: true
}]
});
}
try {
// Reliable Data Channels not yet supported in Chrome
sendChannel = localPeerConnection.createDataChannel('sendDataChannel', {
reliable: false
});
} catch (e) {
}
localPeerConnection.onicecandidate = gotLocalCandidate;
sendChannel.onopen = handleSendChannelStateChange;
sendChannel.onclose = handleSendChannelStateChange;
if (window.mozRTCPeerConnection) {
window.remotePeerConnection = new mozRTCPeerConnection(servers, {
optional: [{
RtpDataChannels: true
}]
});
} else {
window.remotePeerConnection = new webkitRTCPeerConnection(servers, {
optional: [{
RtpDataChannels: true
}]
});
}
remotePeerConnection.onicecandidate = gotRemoteIceCandidate;
remotePeerConnection.ondatachannel = gotReceiveChannel;
// Firefox seems to require an error callback
localPeerConnection.createOffer(gotLocalDescription, function (err) {
});
}
function sendData () {
sendChannel.send(chatWindowMessage.value);
}
function gotLocalDescription (desc) {
localPeerConnection.setLocalDescription(desc);
remotePeerConnection.setRemoteDescription(desc);
// Firefox seems to require an error callback
remotePeerConnection.createAnswer(gotRemoteDescription, function (err) {
});
}
function gotRemoteDescription (desc) {
remotePeerConnection.setLocalDescription(desc);
localPeerConnection.setRemoteDescription(desc);
}
function gotLocalCandidate (event) {
if (event.candidate) {
remotePeerConnection.addIceCandidate(event.candidate);
}
}
function gotRemoteIceCandidate (event) {
if (event.candidate) {
localPeerConnection.addIceCandidate(event.candidate);
}
}
function gotReceiveChannel (event) {
receiveChannel = event.channel;
receiveChannel.onmessage = handleMessage;
receiveChannel.onopen = handleReceiveChannelStateChange;
receiveChannel.onclose = handleReceiveChannelStateChange;
}
function handleMessage (event) {
var chatNewThread = document.createElement('li'),
chatNewMessage = document.createTextNode(event.data);
// Add message to chat thread and scroll to bottom
chatNewThread.appendChild(chatNewMessage);
chatThread.appendChild(chatNewThread);
chatThread.scrollTop = chatThread.scrollHeight;
// Clear text value
chatWindowMessage.value = '';
}
function handleSendChannelStateChange () {
var readyState = sendChannel.readyState;
if (readyState == 'open') {
chatWindowMessage.disabled = false;
chatWindowMessage.focus();
chatWindowMessage.placeholder = "";
} else {
chatWindowMessage.disabled = true;
}
}
function handleReceiveChannelStateChange () {
var readyState = receiveChannel.readyState;
}
站长提示:
1. 苦力吧素材官方QQ群:
950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励
2K币。
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服