# -*- coding: utf8 -*-import jsonimport requestsdef main_handler(event, context):print('Start Register function')print("event is %s"%event)retmsg = {}global connectionIDif 'requestContext' not in event.keys():return {"errNo":101, "errMsg":"not found request context"}if 'websocket' not in event.keys():return {"errNo":102, "errMsg":"not found websocket"}connectionID = event['websocket']['secConnectionID']retmsg['errNo'] = 0retmsg['errMsg'] = "ok"retmsg['websocket'] = {"action":"connecting","secConnectionID":connectionID}if "secWebSocketProtocol" in event['websocket'].keys():retmsg['websocket']['secWebSocketProtocol'] = event['websocket']['secWebSocketProtocol']if "secWebSocketExtensions" in event['websocket'].keys():ext = event['websocket']['secWebSocketExtensions']retext = []exts = ext.split(";")print(exts)for e in exts:e = e.strip(" ")if e == "permessage-deflate":#retext.append(e)passif e == "client_max_window_bits":#retext.append(e+"=15")passretmsg['websocket']['secWebSocketExtensions'] = ";".join(retext)print("connecting \\n connection id:%s"%event['websocket']['secConnectionID'])print(retmsg)return retmsg
# -*- coding: utf8 -*-import jsonimport requestsg_connectionID = 'xxxx' #转发消息到某个特定的 websocket 连接sendbackHost = "http://set-7og8wn64.cb-beijing.apigateway.tencentyun.com/api-xxxx" #API 网关的反向推送地址,在下一步 API 创建好后才能拿到#主动向 Client 端推送消息def send(connectionID,data):retmsg = {}retmsg['websocket'] = {}retmsg['websocket']['action'] = "data send"retmsg['websocket']['secConnectionID'] = connectionIDretmsg['websocket']['dataType'] = 'text'retmsg['websocket']['data'] = json.dumps(data)print("send msg is %s"%retmsg)r = requests.post(sendbackHost, json=retmsg)def main_handler(event, context):print('Start Transmission function')print("event is %s"%event)if 'websocket' not in event.keys():return {"errNo":102, "errMsg":"not found web socket"}for k in event['websocket'].keys():print(k+":"+event['websocket'][k])# 发送内容给某个客户端#connectionID = event['websocket']['secConnectionID']data = event['websocket']['data']send(g_connectionID,data)return event
import jsonimport requestsg_connectionID = 'xxxx' #转发消息到某个特定的 websocket 连接sendbackHost = "http://set-7og8wn64.cb-beijing.apigateway.tencentyun.com/api-xxxx" #API 网关的反向推送地址,在下一步 API 创建好后才能拿到#主动发送断开信息def close(connectionID):retmsg = {}retmsg['websocket'] = {}retmsg['websocket']['action'] = "closing"retmsg['websocket']['secConnectionID'] = connectionIDr = requests.post(sendbackHost, json=retmsg)return retmsgdef main_handler(event, context):print('Start Delete function')print("event is %s"%event)if 'websocket' not in event.keys():return {"errNo":102, "errMsg":"not found web socket"}for k in event['websocket'].keys():print(k+":"+event['websocket'][k])#close(g_connectionID)return event
本页内容是否解决了您的问题?