繁体   English   中英

React-Native上的连接升级

[英]Connection Upgrade on React-Native

所以我目前正在使用React-Native开发一个应用程序,我必须实现Websocket。 现在,我正在尝试将我的应用程序连接到Mattermost。 对于ws,我们必须从html get请求开始,该请求将升级为ws请求。 范例:

URL: https://URL/api/v3/users/websocket
Méthod : GET
Adress : 10.10.10.10:8080
Code d’état : 200

Response (39 o) 
Server  "nginx"
Date    "Tue, 08 Aug 2017 12:33:21 GMT"
Connection  "upgrade"
Upgrade "websocket"
Sec-WebSocket-Accept    "vG2CYVx3/p+6ei/8e5fGMN8qq7A="

Request's header (769 o)    
Host    "URL"
User-Agent  "Mozilla/5.0 (Windows NT 6.1; … Gecko/20100101 Firefox/54.0"
Accept  "text/html,application/xhtml+x…lication/xml;q=0.9,*/*;q=0.8"
Accept-Language "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
Accept-Encoding "gzip, deflate, br"
Sec-WebSocket-Version   "13"
Origin  "URL"
Sec-WebSocket-Extensions    "permessage-deflate"
Sec-WebSocket-Key   "HsX9INf0fr7x0cQAglMyjg=="
Cookie  "MMAUTHTOKEN=7xaaaaaaaaahezhatnwxwih;"
Connection  "keep-alive, Upgrade"
Upgrade "websocket"

所以我的问题是如何在react上进行可升级的连接,并使用它来建立wss连接。

目前,我正在使用axios进行我的html请求和用于我的websocket连接的socket.io。

好的,找到解决方案。 刚刚下载了驱动程序并将其包含在我的应用程序中:

import Websocket from "../Applications/websocket_client"

export default class SecWatt extends Component {
    constructor(props){
        super(props);
    }

    componentDidMount(){
        webs = new Websocket();
        webs.setEventCallback(printEvent);
        webs.initialize("wss://*MattermostURL*/api/v3/users/websocket" , "YourToken");
    }
    render()
    {
        return(
            <View>
            </View>
        );
    }    
}

function printEvent(msg){
        console.log(msg);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM