from sseclient import SSEClient
import time


def get_expId(url,sessionId,streamId,token):
    '''
    url:sse连接地址；
    sessionId：会话Id
    streamId：本次对话Id
    token：凭据
    '''
    params = {
        'sessionId':sessionId,
        'streamId':streamId,
        'access_token':token
    }
    opdata = ""
    messages = SSEClient(url=url,params=params)
    starttime = time.time()
    for msg in messages:
        # 处理每个接收到的消息
        localtime = time.time()
        if localtime > starttime + 180:
            opdata = "获取探索id超时"
            break
        if 'EXP' in msg.data:
            # print("Received Message:", msg.data)
            opdata = msg.data
            break
    return opdata


# print(get_expId('http://bitagent.sit.ninetechone.com/api/chat/read','CB_01688938df2f4c30a81fe6b6c5ca7016','CB_261b4296d3fa4fe1bdb9fc638580d185','Bearer c1391926-e8ba-46c0-a0d1-c5e3c38503da'))
# print(get_expId('http://bitagent.sit.ninetechone.com/api/chat/read','CB_05ae462bcf704b5aa990c0db9b3496cb','CB_ec83a379621a4eddb7360fcd7272b1e0','Bearer c1391926-e8ba-46c0-a0d1-c5e3c38503da'))
