Comments (1)
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)
- activej-Inject, When the return value of a method has a generic HOT 1
- Bug :java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 248 out of bounds for byte[234] HOT 2
- activej-Inject, How to omit `getInstanceInjector(...).injectInto(...)` HOT 2
- Cannot use curly bracket in url for path param HOT 1
- How to use the provided JSON serializer? HOT 1
- How to register with Eureka registry, export OpenTelemetry Tracings and Metrics? HOT 5
- ActiveJ Serializer - Non-static POJOs? HOT 6
- HTTP server Middleware HOT 3
- Constructor not found error for classes without zero-argument constructor, like BigDecimal, Calendar. HOT 1
- Binding automatically between inteface and implementation in DI HOT 2
- Make HTTPResponse's primary response code non-final HOT 4
- Websocket Accept/WriteFrame/WriteMessage not processing more than 2 writes HOT 9
- Suggestion - Nightly Snapshots? HOT 1
- Serializer sometimes decodes a wrong class for RPC request HOT 7
- Any sample to declare local varible? HOT 2
- Happen error while uploading file HOT 2
- Release plan for v5.5 and v6 HOT 3
- Add Query Parameters? HOT 6
- RPCClient lock port on local HOT 1
- Version 6 does not support ipv6, version 5.5 supported ipv6 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from activej.