如何使用聊天应用程序的 socket.io 将反应本机应用程序连接到实时 URL 服务器?



这是为套接字连接实现的代码,但它不起作用。 我想在反应本机中创建聊天消息传递应用程序,其中包含使用我们 Web 端服务器后端的实时更新。我正在尝试将我的应用程序连接到套接字io,但它不起作用。

我已经使用socket.IO-client来响应本机:

import SocketIOClient from 'socket.IO-client'
export default Class Socket extends Component{
constructor(){
super();
var Live_URL = "https://xxx.xxxxxxx.xxx/"
this.socket = SocketIOClient(Live_URL);
}
componentDidMount(){
this.socket.connect();
this.socket.on("connect", () => {
console.log('connection.')
});
}
}

这是服务器端代码。

module.exports = function(app, httpServer) {
var socketioJWT = require('socketio-jwt');
var io = require("socket.io").listen(httpServer);
var clients = [];
var connectedClientSocketIds = []; //for store socket all opened ids.
var clientsStatus = {};
io.use(socketioJWT.authorize({
secret: global.hzConfig.jwtPrivateKey,
handshake: true
}));
//for store all ffmpeg converting process (user for kill running process)
var runningFfmpegCommands = {};
/**
* @description Socket connection in when user going to online
*/
io.sockets.on("connect", function(socket){
console.log(socket)
}

我在控制台中收到这种错误.....

Error: server error
at Socket.onPacket (socket.js:455)
at XHR.<anonymous> (socket.js:278)
at XHR.Emitter.emit (index.js:133)
at XHR.Transport.onPacket (transport.js:149)
at callback (polling.js:144)
at Object.exports.decodePayload (browser.js:384)
at XHR.Polling.onData (polling.js:148)
at Request.<anonymous> (polling-xhr.js:128)
at Request.Emitter.emit (index.js:133)
at Request.onData (polling-xhr.js:302)

拜托,有什么解决方案吗?

在我的反应本机应用程序中对我有用的解决方案:

import io from 'socket.io-client';
componentDidMount() {
const socket = io.connect(url);
socket.on('message', (data) => {});
}

最新更新