【发布时间】:2016-10-31 21:08:32
【问题描述】:
我正在尝试设置我的应用程序与 socket.io 聊天并使用 https。它在我使用 http 时有效,但由于安全原因已更改,我无法再连接到我的聊天服务器。
其实这个应该没什么大不了的,也许有人遇到过类似的问题?
从我的 Web 前端连接到我的聊天服务器效果很好,但尝试下面的代码并不能连接到 Android 和 iOS:
import './UserAgent.js';
import io from 'socket.io-client/socket.io';
const connectionOptions = {
jsonp : false,
transports: ['websocket'],
secure : true
};
export class App extends Component {
constructor() {
super();
this.state = {
chatThings: ''
};
this.chatUrl = 'https://chat.foo.info';
this.socket = io(this.chatUrl, connectionOptions)
}
connectToChatServer() {
let tries = 0;
var that = this;
setInterval(()=> {
that.socket.connect(that.chatUrl, connectionOptions);
console.log('CHAT url', that.chatUrl);
if (that.socket.connected) {
alert('Finally CONNECTED!!!!!!');
that.setState({
connected: socket.connected
});
}
tries = tries + 1;
}, 2500);
this.socket.on('error', (err)=> {
console.log('CHAT: error', err);
});
}
使用最新的 ReactNative 和最新的 socket.io
更新
window.navigator.userAgent = 'ReactNative';
var io = require('../node_modules/socket.io-client/dist/socket.io');
// -----------------------------------------------------------------------------------------------------------------
// Chat
// -----------------------------------------------------------------------------------------------------------------
Backend.prototype.connectToChatServer = function () {
let self = this;
this.dispatch(Actions.connectToChatServer());
const connectionOptions = {
jsonp : false,
secure : true,
transports: ['websocket']
};
log.info('CHAT IO CONNECTION');
this.socket = io(this.chatUrl, connectionOptions);
function authenticate() {
self.socket.emit('authenticate', {token: Store.getState().User.token});
}
........
实际上我唯一改变的是更新套接字 io 并将本机反应到最新的并使用来自 ReactNative 的 UserAgent。我希望这对任何人都有帮助
"socket.io-client": "^1.7.1",
"react-native": "^0.29.2", // this version worked, i have tested it on RN 43 too which works as well
【问题讨论】:
-
嗨 BigPun86,现在我遇到了这个问题,你解决了吗?
-
@QuanVo 我希望我的更新对你有所帮助。
标签: javascript reactjs socket.io react-native ssl-certificate