标签:
2015年5月22日 20:20:20 星期五
效果:
这边对微信说话, 浏览器端及时显示语音识别的文字
注意:
在连接socket.io时, 按下浏览器f12, 如果一直有请求不断的刷, 说明socket.io没有连接成功
代码:
node.js server端
1 var module_path = ‘/usr/local/web/node/bin/node_modules/‘; 2 var html = ‘<html> <head> <meta charset="utf-8"> <title>微信接口</title> <script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.js"></script> <script src="http://cdn.bootcss.com/socket.io/1.3.5/socket.io.js"></script> </head> <body> <div id="voice"> 两秒钟同步一次:<br><br> </div> <script type="text/javascript"> var voice = document.getElementById("voice"); var socket = io.connect("http://www.zhangzhibin.com:1337"); socket.on("hello", function(data){console.log(data); socket.emit("hello", {"status":"ok"}); }); socket.on("emit", function(data){var t = data+"<br>"; voice.innerHTML += t; socket.emit("emit", {"get":"data"}); }); </script> </body> </html>‘; 3 //链接redis 4 var redis = require(module_path+‘redis‘); 5 var redis_client = redis.createClient(6379, ‘127.0.0.1‘); 6 redis_client.auth(‘123456‘); 7 redis_client.on(‘error‘, function(error){ 8 console.log(‘redis-error: ‘ + error); 9 }); 10 11 //创建服务器 12 var http = require(‘http‘); 13 var url = require(‘url‘); 14 var fs = require(‘fs‘); 15 var ch = false; 16 var server = http.createServer(function (req, res) { 17 //获取请求参数 18 var objReqArg = url.parse(req.url, true).query; 19 ch = objReqArg.openid; 20 if (ch) { 21 redis_client.subscribe(ch, function(e){ 22 console.log(‘channel: ‘+ ch); 23 }); 24 }; 25 //返回 26 res.writeHead(200, {‘Content-Type‘: ‘text/html‘}); 27 res.end(html); 28 29 }); 30 server.listen(1337, ‘www.zhangzhibin.com‘); 31 32 //创建服务器 33 var io = require(module_path + ‘socket.io‘).listen(server); 34 io.sockets.on(‘connection‘, function(socket){ 35 socket.on(‘hello‘, function(data){ 36 console.log(data); 37 }); 38 socket.emit(‘hello‘, function(data){ 39 console.log(data); 40 }); 41 redis_client.on(‘message‘, function(error, msg){ 42 socket.emit(‘emit‘, msg); 43 }); 44 }); 45 46 console.log(‘Server running at http://www.zhangzhibin.com:1337‘);
浏览器端html代码:
就是上边代码的第二行变量 var html=‘....‘
1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>微信接口</title> 5 <script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.js"></script> 6 <script src="http://cdn.bootcss.com/socket.io/1.3.5/socket.io.js"></script> 7 </head> 8 <body> 9 <div id="voice"> 两秒钟同步一次:<br><br> </div> 10 <script type="text/javascript"> 11 var voice = document.getElementById("voice"); 12 var socket = io.connect("http://www.zhangzhibin.com:1337"); 13 socket.on("hello", function(data){ console.log(data); socket.emit("hello", {"status":"ok"}); }); 14 socket.on("emit", function(data){ var t = data+"<br>"; voice.innerHTML += t; socket.emit("emit", {"get":"data"}); }); 15 </script> 16 </body> 17 </html>
微信端:
注意一点, 在语音回调函数中, 将语音识别的结果publish到某一个频道上即可
这时, 会触发上边第41行代码, 将文字发完浏览器端
下边是微信回调的代码:
1 public function voice() 2 { 3 // 每次发送消息都会post 来一份签名相关的数据 4 // $echostr = $this->checkSignature(); 5 // exit($echostr); 6 7 preg_match(‘#<FromUserName><!\[CDATA\[([a-zA-Z0-9_]+)\]#‘, $GLOBALS[‘HTTP_RAW_POST_DATA‘], $matches1); 8 preg_match(‘#<Recognition><!\[CDATA\[([^\]]*)\]#‘, $GLOBALS[‘HTTP_RAW_POST_DATA‘], $matches2); 9 $openid = !empty($matches1[1]) ? $matches1[1] : ‘0‘; 10 $text = !empty($matches2[1]) ? $matches2[1] : ‘没听清...‘; 11 12 $objRedis = iredis::getInstance(); 13 $objRedis->publish($openid, $text); 14 15 }
测试方法:
1. 关注我的微信公众号"平夏"
2. 发送消息"主播_username"
3. pc端打开 http://www.zhangzhibin.com/wechat/index/zhubolist
4. 点击你刚才输入的用户名, 进入你的对话页面
5. 对微信发送语音消息, 即可在刚才的浏览器页面看到语音识别结果
标签:
原文地址:http://www.cnblogs.com/iLoveMyD/p/4523241.html