React-Native上的连接升级

时间:2017-08-08 12:01:43

标签: javascript rest reactjs react-native websocket

所以我目前正在使用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为我的websocket连接创建html请求和socket.io。

1 个答案:

答案 0 :(得分:0)

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

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);
    }