socket_protobuf
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -33,8 +33,8 @@ "use strict"; | ||
async onMessage(pkg) { | ||
const handler = this.handlerMgr.getHandler(pkg.cmdType); | ||
const handler = this.handlerMgr.getHandler(pkg.cmdId); | ||
try { | ||
await handler.execute(pkg.cmdType, pkg); | ||
await handler.execute(pkg.cmdId, pkg); | ||
} | ||
catch (e) { | ||
console.error("句柄解析错误:" + pkg.cmdType + ":" + (e.stack || e)); | ||
console.error("句柄解析错误:" + pkg.cmdId + ":" + (e.stack || e)); | ||
} | ||
@@ -41,0 +41,0 @@ } |
@@ -6,8 +6,8 @@ import { ServerSocket } from "../ServerSocket"; | ||
/** | ||
* @param cmdType | ||
* @param cmdId | ||
* @param msg 类型应是Buffer,只是protobufjs返回是Buffer的超类Uint8Array | ||
*/ | ||
protected sendMsg(cmdType: number, msg: Uint8Array): void; | ||
protected sendMsg(cmdId: number, msg: Uint8Array): void; | ||
setServer(server: ServerSocket): void; | ||
abstract execute(cmdID: number, pkg: SocketPackage): Promise<void>; | ||
abstract execute(cmdId: number, pkg: SocketPackage): Promise<void>; | ||
} |
@@ -5,7 +5,7 @@ "use strict"; | ||
/** | ||
* @param cmdType | ||
* @param cmdId | ||
* @param msg 类型应是Buffer,只是protobufjs返回是Buffer的超类Uint8Array | ||
*/ | ||
sendMsg(cmdType, msg) { | ||
this.sender.sendTypeBuff(cmdType, msg); | ||
sendMsg(cmdId, msg) { | ||
this.sender.sendTypeBuff(cmdId, msg); | ||
} | ||
@@ -12,0 +12,0 @@ setServer(server) { |
@@ -6,3 +6,3 @@ import { Handler } from "./Handler"; | ||
serverInfo: Client; | ||
execute(cmdID: number, pkg: SocketPackage): Promise<void>; | ||
execute(cmdId: number, pkg: SocketPackage): Promise<void>; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
class ServerInfoHandler extends Handler_1.Handler { | ||
async execute(cmdID, pkg) { | ||
async execute(cmdId, pkg) { | ||
const server = Server_1.PServer.decode(pkg.protoBuf); | ||
@@ -9,0 +9,0 @@ this.serverInfo.onGetServerInfo(server); |
@@ -21,3 +21,3 @@ import { Socket } from "net"; | ||
private removeEvent; | ||
sendTypeBuff(cmdType: number, buff: Uint8Array): boolean; | ||
sendTypeBuff(cmdType: number, buff: Uint8Array | Buffer): boolean; | ||
sendBuff(buff: Buffer): boolean; | ||
@@ -24,0 +24,0 @@ private onReceivePkg; |
@@ -35,3 +35,3 @@ "use strict"; | ||
catch (e) { | ||
console.log("句柄解析错误:" + pkg.cmdType + ":" + (e.stack || e)); | ||
console.log("句柄解析错误:" + pkg.cmdId + ":" + (e.stack || e)); | ||
} | ||
@@ -44,3 +44,3 @@ finally { | ||
async pkg2handler(pkg) { | ||
let cmdType = pkg.cmdType; | ||
let cmdType = pkg.cmdId; | ||
let handler = this.server.handlerMgr.getHandler(cmdType); | ||
@@ -47,0 +47,0 @@ if (handler != null) { |
import { SocketPackage } from "../../SocketPackage"; | ||
import { Handler } from "./Handler"; | ||
export declare class ClientHandler extends Handler { | ||
execute(cmdID: number, socketID: number, pkg: SocketPackage): Promise<void>; | ||
execute(cmdID: number, socketId: number, pkg: SocketPackage): Promise<void>; | ||
} |
@@ -6,10 +6,8 @@ "use strict"; | ||
class ClientHandler extends Handler_1.Handler { | ||
async execute(cmdID, socketID, pkg) { | ||
async execute(cmdID, socketId, pkg) { | ||
const client = Server_1.PServer.decode(pkg.protoBuf); | ||
console.log(client.ip, client.port, client.machineId, client.name, client.version, client.other1.length, client.other2.length); | ||
const server = new Server_1.PServer(); | ||
const buff = Server_1.PServer.encode(server).finish(); | ||
this.sendMsg(cmdID, socketID, buff); | ||
this.server.onClientInfo(socketId, client); | ||
this.sendMsg(cmdID, socketId, this.server.serverInfo); | ||
} | ||
} | ||
exports.ClientHandler = ClientHandler; |
@@ -5,8 +5,8 @@ import { SocketPackage } from "../../SocketPackage"; | ||
export declare abstract class Handler { | ||
private server; | ||
protected server: ServerListener; | ||
setServer(server: ServerListener): void; | ||
protected getMyClient(socketID: number): ClientSocket; | ||
doHandler(socketID: number, cmdID: number, pkg: SocketPackage): Promise<void>; | ||
protected abstract execute(cmdID: number, socketID: number, pkg: SocketPackage): Promise<void>; | ||
sendMsg(cmdType: number, socketID: number, buff: any): void; | ||
protected getMyClient(socketId: number): ClientSocket; | ||
doHandler(socketId: number, cmdId: number, pkg: SocketPackage): Promise<void>; | ||
protected abstract execute(cmdId: number, socketId: number, pkg: SocketPackage): Promise<void>; | ||
sendMsg(cmdId: number, socketId: number, buff: Uint8Array | Buffer): void; | ||
} |
@@ -8,12 +8,12 @@ "use strict"; | ||
} | ||
getMyClient(socketID) { | ||
return this.server.getClient(socketID); | ||
getMyClient(socketId) { | ||
return this.server.getClient(socketId); | ||
} | ||
async doHandler(socketID, cmdID, pkg) { | ||
await this.execute(cmdID, socketID, pkg); | ||
async doHandler(socketId, cmdId, pkg) { | ||
await this.execute(cmdId, socketId, pkg); | ||
} | ||
sendMsg(cmdType, socketID, buff) { | ||
let pkg = new SocketPackage_1.SocketPackage(cmdType); | ||
sendMsg(cmdId, socketId, buff) { | ||
let pkg = new SocketPackage_1.SocketPackage(cmdId); | ||
pkg.putBytes(buff); | ||
let client = this.getMyClient(socketID); | ||
let client = this.getMyClient(socketId); | ||
if (client) { | ||
@@ -23,3 +23,3 @@ client.sendPkg(pkg); | ||
else { | ||
console.warn(`client为空,cmdType=${cmdType}&socketID=${socketID}`); | ||
console.warn(`client为空,cmdType=${cmdId}&socketID=${socketId}`); | ||
} | ||
@@ -26,0 +26,0 @@ } |
import { ClientSocket } from "./ClientSocket"; | ||
import { HandlerMgr } from "./handler/HandlerMgr"; | ||
import { PServer } from "../Server"; | ||
export declare class ServerListener { | ||
private static index; | ||
private readonly port; | ||
private readonly ipv4; | ||
readonly serverInfo: Buffer; | ||
private server; | ||
@@ -13,3 +16,4 @@ readonly clientMap: Map<number, ClientSocket>; | ||
private onDisconnectClient; | ||
constructor(port: number, ipv4: boolean); | ||
onClientInfo: (socketId: number, PServer: any) => void; | ||
constructor(port: number, ipv4?: boolean, serverInfo?: PServer); | ||
/** | ||
@@ -19,4 +23,5 @@ * 侦听客户端连接与断开 | ||
* @param onDisconnectClient 侦听客户端断开 | ||
* @param onClientInfo 客户端发送proto连接数据 | ||
*/ | ||
startServer(onConnectClient: (client: ClientSocket) => void, onDisconnectClient: (socketId: number, client: ClientSocket) => void): void; | ||
startServer(onConnectClient: (client: ClientSocket) => void, onDisconnectClient: (socketId: number, client: ClientSocket) => void, onClientInfo?: (socketId: number, PServer: any) => void): void; | ||
private tryGetIpv4; | ||
@@ -26,4 +31,5 @@ private startListen; | ||
removeClient(socketId: number): void; | ||
private _onClientInfo; | ||
getClient(socketId: number): ClientSocket; | ||
close(): void; | ||
} |
@@ -5,4 +5,5 @@ "use strict"; | ||
const HandlerMgr_1 = require("./handler/HandlerMgr"); | ||
const Server_1 = require("../Server"); | ||
class ServerListener { | ||
constructor(port, ipv4) { | ||
constructor(port, ipv4 = true, serverInfo = null) { | ||
this.clientMap = new Map(); | ||
@@ -13,2 +14,17 @@ this.lastClientId = 0; | ||
this.ipv4 = ipv4; | ||
if (null == serverInfo) { | ||
const os = require("os"); | ||
//物理服务器配制唯一编号 | ||
const machineId = ServerListener.index++ * 100 + Math.floor(Date.now() / 1000); | ||
serverInfo = new Server_1.PServer({ | ||
name: os.hostname(), | ||
version: machineId.toString(32), | ||
ip: ipv4 ? "127.0.0.1" : "", | ||
port: port, | ||
machineId: machineId, | ||
other1: [], | ||
other2: [] //其它参数2 | ||
}); | ||
} | ||
this.serverInfo = Server_1.PServer.encode(serverInfo).finish(); | ||
this.handlerMgr = new HandlerMgr_1.HandlerMgr(this); | ||
@@ -20,6 +36,8 @@ } | ||
* @param onDisconnectClient 侦听客户端断开 | ||
* @param onClientInfo 客户端发送proto连接数据 | ||
*/ | ||
startServer(onConnectClient, onDisconnectClient) { | ||
startServer(onConnectClient, onDisconnectClient, onClientInfo = null) { | ||
this.onConnectClient = onConnectClient; | ||
this.onDisconnectClient = onDisconnectClient; | ||
this.onClientInfo = null == onClientInfo ? this._onClientInfo.bind(this) : onClientInfo; | ||
if (this.ipv4) { | ||
@@ -97,2 +115,5 @@ this.tryGetIpv4(this.startListen.bind(this)); | ||
} | ||
_onClientInfo(socketId, client) { | ||
console.log(client.toJSON().toString() + `\n socket id:${socketId} 端口:${this.port}`); | ||
} | ||
getClient(socketId) { | ||
@@ -109,2 +130,3 @@ return this.clientMap.get(socketId); | ||
} | ||
ServerListener.index = 1; | ||
exports.ServerListener = ServerListener; |
@@ -6,3 +6,3 @@ /** | ||
private _buffer; | ||
private readonly _cmd_type; | ||
private readonly cmd_id; | ||
private _info_length; | ||
@@ -12,10 +12,10 @@ static headLength: number; | ||
static idxCmdId: number; | ||
readonly cmdType: number; | ||
constructor(cmdType?: number); | ||
readonly cmdId: number; | ||
constructor(cmdId?: number); | ||
private initHead; | ||
can_send: boolean; | ||
setBuffer(buffer: Buffer, _info_length: number): void; | ||
putBytes(buffer: Uint8Array): void; | ||
putBytes(buffer: Uint8Array | Buffer): void; | ||
readonly buffer: Buffer; | ||
readonly protoBuf: Buffer; | ||
} |
@@ -7,9 +7,9 @@ "use strict"; | ||
class SocketPackage { | ||
constructor(cmdType = -1) { | ||
constructor(cmdId = -1) { | ||
this.can_send = false; | ||
this._cmd_type = cmdType; | ||
this.cmd_id = cmdId; | ||
this.initHead(); | ||
} | ||
get cmdType() { | ||
return this._cmd_type; | ||
get cmdId() { | ||
return this.cmd_id; | ||
} | ||
@@ -19,3 +19,3 @@ initHead() { | ||
this._buffer.writeUInt32BE(0, SocketPackage.idxProtoLen); | ||
this._buffer.writeUInt32BE(this._cmd_type, SocketPackage.idxCmdId); | ||
this._buffer.writeUInt32BE(this.cmd_id, SocketPackage.idxCmdId); | ||
} | ||
@@ -22,0 +22,0 @@ setBuffer(buffer, _info_length) { |
{ | ||
"name": "socket_protobuf", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "socket发送protobuf二进制流的SDK", | ||
@@ -5,0 +5,0 @@ "main": "App.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43180
1196