var cons = new Array()var ws = require('ws').Servervar server = new ws({ port: 8888 })server.on('connection', function (ws) {console.log('new connection founded successfully')cons.push(ws)ws.on('message', function (data) {for (var i = 0i <cons.lengthi++) {cons[i].send(data)}})ws.on('close', function () {for (var i = 0i <cons.lengthi++) {if (cons[i] == ws) cons.splice(i, 1)}})})console.log('websocket-server running...')
接下来为了更容易理解, 我在这分为两个页面,一个为视频者页面,另一个是观看者页面
下面代码为视频者页面:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /><title></title><script src="jQuery_1.8.2.min.js"></script><script type="text/javascript">$(function () {var video = document.getElementById("video")var canvas = document.getElementById("canvas"),context = canvas.getContext("2d")var wif (navigator.webkitGetUserMedia) {navigator.webkitGetUserMedia({ video: true }, function (stream) {video.src = window.webkitURL.createObjectURL(stream)video.play()w = new WebSocket(url)w.onopen = function () {sendImg()}w.onmessage = function (e) {sendImg()}}, function () {console.log("video error")})var host = 'localhost'var port = 8888var url = 'ws://' + host + ':' + port + '/'function sendImg() {context.drawImage(video, 0, 0, 320, 320)var imgData = canvas.toDataURL()w.send(imgData)}}})</script></head><body><video id="video" width="640" height="480" style="background:#000" autoplay></video><canvas style="display:none" id="canvas" width="320" height="320"></canvas></body></html>
接下来是观看者页面:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /><title></title><script src="jQuery_1.8.2.min.js"></script><script>$(function () {var host = 'localhost'var port = 8888var url = 'ws://' + host + ':' + port + '/'var w = new WebSocket(url)w.onmessage = function (e) {$("#canvas").attr("src", e.data)}})</script></head><body><img id="canvas" width="320" height="320" /></body></html>注:希望可以帮到你!
用javascript获取语音识别这个事件的结束,可以使用onwebkitspeechchange<input type=”text” speech x-webkit-speech onwebkitspeechchange=”alert(this.value)” />
HTML5之语音识别实例代码
<input type="text" x-webkit-speech id="d1" lang="zh-CN" x-webkit-grammar="bUIltin:search" onwebkitspeechchange="foo()"/>
<script>
function foo(){
var n = document.getElementById("d1").value
if(n == "百度"){
window.location.href = "http://www.baidu.com"
}else{
window.location.href = "http://www.ahsdxy.ah.edu.cn/"
}
}
</script>
说明:
1)x-webkit-speech:语音识别支持属性
<input type="text" x-webkit-speech/>
2)lang:设置语言种类,比如汉语:lang="ch-CN"
<input type="text" x-webkit-speech lang="ch-CN"/>
3) x-webkit-grammar :语音输入语法
比如: x-webkit-grammar="bUIltin:search"使得语音输入的内容尽量靠近搜索内容,去除多余的字符,例如“的、啦”等
<input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search"/>
4) onwebkitspeechchange :语音输入事件,当语音改变时触发
比如:onwebkitspeechchange="foo()" ,当停止语音时,会触发js中的foo()函数
<input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search"
onwebkitspeechchange="foo()"/>
此时,需要写相应的JavaScript函数foo()
<script>
function foo(){
//函数体,如下:
alert(8)
}
</script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)