feat: channel add ext data

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-27 18:14:17 +08:00
parent 33d29271f9
commit 97877832c4
4 changed files with 14 additions and 7 deletions

View File

@ -5,7 +5,7 @@ export namespace channel {
* handle plugin message * handle plugin message
* @param data byte[] * @param data byte[]
*/ */
export type ChannelListener = (data: any) => void export type ChannelListener = (data: any, exts?: any) => void
@injectable() @injectable()
export abstract class Channel { export abstract class Channel {

View File

@ -15,8 +15,8 @@ export class BukkitChannel extends channel.Channel {
} }
register(channel: string, listener: channel.ChannelListener) { register(channel: string, listener: channel.ChannelListener) {
Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, new PluginMessageListener({ Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, new PluginMessageListener({
onPluginMessageReceived: (/**String */ var1, /**Player */ var2, /**byte[] */var3) => { onPluginMessageReceived: (/**String */ channel, /**Player */ player, /**byte[] */data) => {
listener(var3) listener(data, { channel, player, data })
} }
})); }));
Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel); Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel);

View File

@ -1,18 +1,25 @@
import { channel } from '@ms/api' import { channel, event } from '@ms/api'
import { provideSingleton } from '@ms/container' import { provideSingleton, inject } from '@ms/container'
const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy() const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
@provideSingleton(channel.Channel) @provideSingleton(channel.Channel)
export class BungeeChannel extends channel.Channel { export class BungeeChannel extends channel.Channel {
@inject(event.Event)
private eventManager: event.Event;
send(player: any, channel: string, data: any) { send(player: any, channel: string, data: any) {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
register(channel: string, listener: channel.ChannelListener) { register(channel: string, listener: channel.ChannelListener) {
Bungee.registerChannel(channel); Bungee.registerChannel(channel);
console.console('§6[§eWARN§6] §eBungeeCord Channel only registerChannel you need self hanler PluginMessageEvent!') // console.console('§6[§eWARN§6] §eMiaoScript channel in BungeeCord only register. you need self hanler PluginMessageEvent!')
return this.eventManager.listen({ description: { name: channel } }, "PluginMessageEvent", (event: net.md_5.bungee.api.event.PluginMessageEvent) => {
listener(event.getData(), event)
})
} }
unregister(channel: string, listener: any) { unregister(channel: string, listener: any) {
Bungee.unregisterChannel(channel); Bungee.unregisterChannel(channel);
listener.off();
} }
} }

View File

@ -25,7 +25,7 @@ export class SpongeChannel extends channel.Channel {
} }
let innerListener = new RawDataListener({ let innerListener = new RawDataListener({
handlePayload: (/* ChannelBuf */ data: any, /**RemoteConnection */ connection: any, /**Platform.Type */ side: any) => { handlePayload: (/* ChannelBuf */ data: any, /**RemoteConnection */ connection: any, /**Platform.Type */ side: any) => {
listener(data.readBytes(data.available())) listener(data.readBytes(data.available()), { data, connection, side })
} }
}) })
this.channelMap.get(channel).addListener(innerListener); this.channelMap.get(channel).addListener(innerListener);