这是为套接字连接实现的代码,但是没有用。 我想创建一个聊天消息应用程序,以响应本机并使用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)
请问有什么解决办法吗?
答案 0 :(得分:0)
在我的本机应用程序中对我有用的解决方案:
import io from 'socket.io-client';
componentDidMount() {
const socket = io.connect(url);
socket.on('message', (data) => {});
}