ElectroServer 5 HelloWorld 服务端详细说明

    技术2024-09-30  54

    package com.electrotank.electroserver5.examples.helloworld; import com.electrotank.electroserver5.extensions.BasePlugin; import com.electrotank.electroserver5.extensions.api.ScheduledCallback; import com.electrotank.electroserver5.extensions.api.value.EsObject; import com.electrotank.electroserver5.extensions.api.value.EsObjectRO;

    包和引用。

     

    public class HelloWorld extends BasePlugin { private static final String HELLO_WORLD_MESSAGE = "hwm"; private static final String MESSAGE = "Hello world!"; private static final int DURATION_MS = 5000; private int callback = -1;

    需要继承BasePlugin类。这个类具体是啥,以后专门研究

     

    String:HELLO_WORLD_MESSAGE   返回给客户端的key。

    String:MESSAGE     返回给客户端的value。

    int:DURATION_MS    延迟多少秒执行 scheduleExecution()方法。

    int:callback    scheduleExecution()方法返回值。

     

    @Override public void init( EsObjectRO ignored ) { callback = getApi().scheduleExecution(DURATION_MS, -1, new ScheduledCallback() { public void scheduledCallback() { sayHello(); } }); }

    覆盖父类init()方法。

    过5000毫秒开始不停创建 ScheduledCallback 实例。该实例每次创建,执行 sayHello()方法。

     

     

    @Override public void destroy() { getApi().cancelScheduledExecution(callback); }

    覆盖父类destroy()方法。

    销毁callback。

     

    private void sayHello() { EsObject message = new EsObject(); message.setString(HELLO_WORLD_MESSAGE, MESSAGE); getApi().sendPluginMessageToRoom(getApi().getZoneId(), getApi().getRoomId(), message); }

    自定义方法。

    创建EsObject()的实例。并且给该实例赋值。最后发送该实例到聊天室中;所有在聊天室的玩家都会收到该信息。

    最新回复(0)