【发布时间】:2017-06-15 09:13:01
【问题描述】:
我正在尝试设置一个通过 JSON 与网络服务器通信的 Websocket 客户端。我在网上没有找到任何好的例子。
我只在网上找到了这段代码:
var ws = new WebSocket('ws://host.com/path');
ws.onopen = () => {
// connection opened
ws.send('something'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log(e.data);
};
ws.onerror = (e) => {
// an error occurred
console.log(e.message);
};
ws.onclose = (e) => {
// connection closed
console.log(e.code, e.reason);
};
我不知道如何将此代码集成到我的应用程序代码中才能运行。 我需要为它安装一些软件包吗? 我正在为我的学习做一个项目,最后我应该有一个测验应用程序,它连接到服务器以获取游戏的问题和答案。
感谢您的帮助!
【问题讨论】:
标签: android json react-native websocket