Giter VIP home page Giter VIP logo

Comments (1)

azzahrah avatar azzahrah commented on May 24, 2024

Thank problem by this sample

public class MyWebsocketLauncher extends Launcher {

    public static final String PROPERTIES_FILE = "http-server.properties";

    @Inject
    AsyncHttpServer httpServer;

    @Provides
    Eventloop eventloop(io.activej.config.Config config, @Optional ThrottlingController throttlingController) {
        return Eventloop.create()
                .withInitializer(ofEventloop(config.getChild("eventloop")))
                .withInitializer(eventloop -> eventloop.withInspector(throttlingController));
    }

    @Provides
    AsyncHttpServer server(Eventloop eventloop, AsyncServlet rootServlet, io.activej.config.Config config) {
        return AsyncHttpServer.create(eventloop, rootServlet)
                //or set port from this
                .withListenPort(7070);
                //.withInitializer(ofHttpServer(config.getChild("http")));
    }

    @Provides
    io.activej.config.Config config() {
        return io.activej.config.Config.create()
                //.with("", io.activej.config.Config.EMPTY)
                //set by this method
                //.with("http.listenAddresses", io.activej.config.Config.ofValue(ofInetSocketAddress(), new InetSocketAddress(9090)))
                .overrideWith(ofClassPathProperties(PROPERTIES_FILE, true))
                .overrideWith(ofSystemProperties("config"));
    }

    @Override
    protected final io.activej.inject.module.Module getModule() {
        return combine(
                ServiceGraphModule.create(),
                JmxModule.create(),
                ConfigModule.create()
                        .withEffectiveConfigLogger(),
                getBusinessLogicModule()
        );
    }

    /**
     * Override this method to supply your launcher business logic.
     */
    protected io.activej.inject.module.Module getBusinessLogicModule() {
        return io.activej.inject.module.Module.empty();
    }

    @Override
    protected void run() throws Exception {
        logger.info("HTTP Server is now available at {}", String.join(", ", httpServer.getHttpAddresses()));
        awaitShutdown();
    }

    @Provides
    AsyncServlet servlet() {
        return RoutingServlet.create()
                .mapWebSocket("/websocket", webSocket -> readMessage(webSocket))
                .mapWebSocket("/", webSocket -> readMessage(webSocket));

    }

    void readMessage(WebSocket websocket) {

        websocket.messageReadChannel()
                .filter(msg -> msg.getType() == WebSocket.Message.MessageType.TEXT)
                .map(WebSocket.Message::getText)
                .streamTo(ChannelConsumer.ofConsumer(msg -> {
                    System.out.println(msg);
                    // sendData(websocket);
                }));

    }

    public static void main(String[] args) throws Exception {
        Eventloop eventloop = Eventloop.create().withCurrentThread();
        MyWebsocketLauncher launcher = new MyWebsocketLauncher();
        launcher.launch(args);
        System.out.println("Server is running");
        System.out.println("You can connect from telnet with command: telnet localhost 9922 or by running csp.TcpClientExample");
        eventloop.run();
    }
//
//    public static void main(String[] args) throws Exception {
//
//        Launcher launcher = new MyWebsocketLauncher() {
//            @Override
//            protected io.activej.inject.module.Module getBusinessLogicModule() {
//                return new AbstractModule() {
//                    @Provides
//                    public AsyncServlet servlet(io.activej.config.Config config) {
//                        String message = config.get("message", "Hello, world!");
//                        return request -> HttpResponse.ok200().withPlainText(message);
//                    }
//                };
//            }
//        };
//
//        launcher.launch(args);
//    }
}

from activej.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.