我们正在处理角度为4的均值堆栈并表达, 在服务器端使用socket.io和在前端使用ng-socket-io 前端代码
import { Injectable } from '@angular/core';
import { Socket, SocketIoModule } from 'ng-socket-io';
import 'rxjs/add/operator/map';
export interface Message {
author: string,
message: string
}
@Injectable()
export class SocketService {
constructor( private mysocket : Socket ) {}
trigger(name,content){
this.mysocket.emit(name,content);
}
getFromEvent() {
return this.mysocket
.fromEvent<any>('chatRes')
.map( data => data );
}
insertActive() {
return this.mysocket
.fromEvent<any>('insertActive')
.map( data => data );
}
insertStop() {
return this.mysocket
.fromEvent<any>('insertStop')
.map( data => data );
}
liveOrderAdd() {
return this.mysocket
.fromEvent<any>('liveOrderAdd')
.map( data => data );
}
orderRemoved() {
return this.mysocket
.fromEvent<any>('orderRemoved')
.map( data => data );
}
updateLiveOrders() {
return this.mysocket
.fromEvent<any>('updateLiveOrders')
.map( data => data );
}
updateActiveOrder() {
return this.mysocket
.fromEvent<any>('updateActiveOrder')
.map( data => data );
}
tradeRates() {
return this.mysocket
.fromEvent<any>('tradeRates')
.map( data => data );
}
updateStopOrder() {
return this.mysocket
.fromEvent<any>('updateStopOrder')
.map( data => data );
}
updateHistory() {
return this.mysocket
.fromEvent<any>('updateHistory')
.map( data => data );
}
}
后端代码为
var socketio = require('socket.io');
var chatDb = require('../model/chat.js');
var users = [];
var connections = [];
module.exports.listen = function(app){
io = socketio.listen(app)
io.on('connection', function(socket){
connections.push(socket);
//Disconnect
socket.on('disconnect', function(data){
console.log(data);
users.splice(users.indexOf(socket.username), 1);
//updateUsernames();
connections.splice(connections.indexOf(socket), 1);
socket.disconnect();
});
socket.on('chatMsg',function(det){
if(det.name && det.message) {
chatDb.create(det,function(err,resData){
if(err === null) {
io.sockets.emit('chatRes',det)
}
})
}
});
socket.on('addUser',function(user){
if(user) {
users.push(user);
socket.join(user)
}
console.log(users);
});
socket.on('connectuser',function(user){
console.log(user,'testing user');
});
});
return io;
}
我们需要从服务器端发出响应,并从客户端获得响应,这是我们应用程序中的重要过程
它对于1或2个连接(用户)正常工作
但是对于数字用户,它会继续断开连接
有ping超时错误
当我们在堆栈溢出中搜索时,有人说需要放置pingTimeout和pingInterval,但是会发生相同的问题
var io = require('./server/helpers/socket').listen(server,{'pingInterval': 100000, 'pingTimeout': 600000})
我们在这里为此感到挣扎,有人为此提供帮助
等待有价值的回复谢谢