在使用webscoket接webapi出错;贴部分代码
let base64 = require('../../utils/base64');
let CryptoJS = require('../../utils/hmac-sha256');
let SocketTask = null
//通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。
function sendSocketMessage(msg) {
var that = this;
console.log('通过 WebSocket 连接发送数据', JSON.stringify(msg))
SocketTask.send({
data: JSON.stringify(msg)
}, function (res) {
console.log('已发送', res)
})
}
Page({
/**
* 页面的初始数据
*/
data: {
socketOpen:false,
common:{
// 公共请求参数
app_id:"5e93bcf3"
},
business:{
language:"zh_cn",
domain:"iat",
accent:"mandarin"
},
sendsData:{
status:0,
format:"audio/L16;rate=16000",
encoding:"raw",
audio:""
},
authorization:'',
date:'',
host:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
geweb(){
var that = this;
let socketOpen = false
SocketTask.onOpen(res => {
socketOpen = true;
console.log('监听 WebSocket 连接打开事件。', res)
this.submitTo()
})
SocketTask.onClose(onClose => {
console.log('监听 WebSocket 连接关闭事件。', onClose)
socketOpen = false;
// this.webSocket()
})
SocketTask.onError(onError => {
console.log('监听 WebSocket 错误。错误信息', onError)
socketOpen = false
})
SocketTask.onMessage(onMessage => {
console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', JSON.parse(onMessage.data))
// var onMessage_data = JSON.parse(onMessage.data)
})
},
webSocket: function () {
// 创建Socket
let wxURL = this.geturl()
console.log(wxURL)
let that = this
let transfer = {}
transfer.common = this.data.common
transfer.business = this.data.business
transfer.data = this.data.sendsData
// transfer =JSON.stringify(transfer)
SocketTask = wx.connectSocket({
url:wxURL,
data:transfer,
header:{
content-type: "application/json;",
host: "iat-api.xfyun.cn",
authorization:that.data.authorization,
date:that.data.date
},
// protocols: ['protocol1'],
method:"GET" ,
success: function (res) {
console.log('WebSocket连接创建', res)
},
fail: function (err) {
wx.showToast({
title: '网络异常!',
})
}
})
that.geweb()
},
// 提交
submitTo: function () {
let transfer = {}
transfer.common = this.data.common
transfer.business = this.data.business
transfer.data = this.data.sendsData
transfer =JSON.stringify(transfer)
// 如果打开了socket就发送数据给服务器
sendSocketMessage(transfer)
},
/* 关闭 */
onHide: function () {
SocketTask.close(function (close) {
console.log('关闭 WebSocket 连接。', close)
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
geturl(){
let apiSecret = '' // 从平台拿
let url = 'wss://iat-api.xfyun.cn/v2/iat';
let apiKey = ''; // 从平台拿
let signature = '';
// let host = 'iat-api.xfyun.cn/v2/iat';
// signature原始字段由 host,date,request-line
// host: $host\ndate: $date\n$request-line
let host = 'iat-api.xfyun.cn'; // 域名
let nowData = new Date()
let date = nowData.toGMTString(); // 时间戳 转一下时间
let reques = 'GET /v2/iat HTTP/1.1';
let signature_origin = 'host: '+host+'\n'+'date: '+date+'\n'+reques
console.log(signature_origin)
// 使用hmac-sha256算法结合apiSecret对signature_origin签名,获得签名后的摘要signature_sha。
// let signature_sha= hmac-sha256(signature_origin,$apiSecret)
let signature_sha = CryptoJS.CryptoJS.HmacSHA256(signature_origin,apiSecret)
// console.log(signature_sha)
signature = base64.Base64.encode(signature_sha)
// console.log(signature)
let authorization_origin = 'api_key='+'"'+apiKey+'"'+', algorithm="hmac-sha256", headers="host date request-line", signature='+'"'+signature+'"' ;
// console.log(authorization_origin)
let authorization = base64.Base64.encode(authorization_origin)
// console.log(authorization)
// let newDate = encodeURI(date)
let newDate = encodeURIComponent(date)
console.log(date,'======',newDate)
this.setData({
authorization:authorization,
date:date,
host:host
})
let callurl = url + '?authorization=' + authorization + '&date=' + newDate + '&host=' +host
// console.log(callurl)
return callurl
},
sayStartHandel(){
console.log('按下')
},
sayStopHandel(){
console.log('松开')
// this.openSocket(wxURL)
this.webSocket()
},
})
{errMsg: "connectSocket:ok", socketTaskId: "1"}
{errMsg: "exception onOpen fail code:20, msg:Invalid HTTP status."}
那个 date的参数格式不对,我也遇到这个问题了,你解决了吗?