secConnectionID
of the WebSocket connection. The secConnectionID
is usually recorded in the persistent storage in this function for reverse push of subsequent data.secConnectionID
. The secConnectionID
is usually cleaned from the persistent storage in this function.secConnectionID
of the connection and the data sent. Business data is usually processed in this function. For example, it determines whether to push data to other secConnectionID
values in the persistent storage.secConnectionID
or disconnect a secConnectionID
, the reverse push address of API Gateway has to be used.main_handler
for various functions.# -*- 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
secConnectionID
to TencentDB or create and associate a chat room.# -*- coding: utf8 -*-import jsonimport requestsg_connectionID = 'xxxx' # Forward the message to a specific WebSocket connectionsendbackHost = "http://set-7og8wn64.cb-beijing.apigateway.tencentyun.com/api-xxxx" # API Gateway's reverse push address, which can be obtained after the API is created in the next step# Actively push the message to the clientdef 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])# Send the content to a specific client#connectionID = event['websocket']['secConnectionID']data = event['websocket']['data']send(g_connectionID,data)return event
secConnectionID
stored in TencentDB.import jsonimport requestsg_connectionID = 'xxxx' # Forward the message to a specific WebSocket connectionsendbackHost = "http://set-7og8wn64.cb-beijing.apigateway.tencentyun.com/api-xxxx" # API Gateway's reverse push address, which can be obtained after the API is created in the next step# Actively send disconnection informationdef 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
secConnectionID
disconnected in this request from TencentDB or force the client of a secConnectionID
to go offline.
Was this page helpful?