html5 asr websocket,ibm cloud - Watson ASR python WebSocket - Stack Overflow

Thanks for the headers information. Here's how it worked for me.

I am using WebSocket-client 0.54.0, which is currently the latest version. I generated a token using

curl -u : "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

Using the returned token in the below code, I was able to make the handshake

import websocket

try:

import thread

except ImportError:

import _thread as thread

import time

import json

def on_message(ws, message):

print(message)

def on_error(ws, error):

print(error)

def on_close(ws):

print("### closed ###")

def on_open(ws):

def run(*args):

for i in range(3):

time.sleep(1)

ws.send("Hello %d" % i)

time.sleep(1)

ws.close()

print("thread terminating...")

thread.start_new_thread(run, ())

if __name__ == "__main__":

# headers["Authorization"] = "Basic " + base64.b64encode(auth.encode()).decode('utf-8')

websocket.enableTrace(True)

ws = websocket.WebSocketApp("wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize",

on_message=on_message,

on_error=on_error,

on_close=on_close,

header={

"X-Watson-Authorization-Token": "})

ws.on_open = on_open

ws.run_forever()

Response:

--- request header ---

GET /speech-to-text/api/v1/recognize HTTP/1.1

Upgrade: websocket

Connection: Upgrade

Host: stream.watsonplatform.net

Origin: http://stream.watsonplatform.net

Sec-WebSocket-Key: Yuack3TM04/MPePJzvH8bA==

Sec-WebSocket-Version: 13

X-Watson-Authorization-Token:

-----------------------

--- response header ---

HTTP/1.1 101 Switching Protocols

Date: Tue, 04 Dec 2018 12:13:57 GMT

Content-Type: application/octet-stream

Connection: upgrade

Upgrade: websocket

Sec-Websocket-Accept: 4te/E4t9+T8pBtxabmxrvPZfPfI=

x-global-transaction-id: a83c91fd1d100ff0cb2a6f50a7690694

X-DP-Watson-Tran-ID: a83c91fd1d100ff0cb2a6f50a7690694

-----------------------

send: b'\x81\x87\x9fd\xd9\xae\xd7\x01\xb5\xc2\xf0D\xe9'

Connection is already closed.

### closed ###

Process finished with exit code 0

According to RFC 6455, the server should respond with 101 Switching protocol,

The handshake from the server looks as follows:

HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Sec-WebSocket-Protocol: chat

Additionally, when I am using ws:// instead of wss://, I am facing the operation timeout issue.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值