socks 0.1.0
socks: ^0.1.0 copied to clipboard
Client and server package for building and consuming HTTP/1.1 web services including RESTful services, WebSocket services, and STOMP 1.2 WebSocket services.
socks #
Client and server library for building and consuming robust HTTP/1.1 web services including RESTful services, WebSocket services, and STOMP 1.2 WebSocket services.
The server-side part of the library includes an HTTP/HTTPS server for hosting static files, and provides metadata annotations to simplify developing REST, WebSocket, and STOMP WebSocket services.
The client-side simplifies connecting with HTTP/HTTPS servers, making REST requests, parsing REST responses, performing WebSocket upgrades, sending STOMP-formatted frames, and parsing received STOMP frames.
HTTP server responding to GET requests #
import "package:socks/server_socks.dart" as server;
// Define REST request handler.
@server.UriPath("/food")
class RestHandler extends server.HttpRequestHandler {
@server.GET("/{maincourse}/{ingredient}")
void getSomething(int requestId, HttpRequest httpRequest, Map<String, String> pathParams) {
print("HTTP GET\nMain Course=${pathParams["maincourse"]}\nIngredient=${pathParams["ingredient"]}");
}
}
// Configure server in its simplest form.
server.Router router = new server.Router();
router.addRequestHandler(new RestHandler());
new server.Server().bind()
.then((server.Server server) {
server.listen((HttpRequest httpRequest) {
router.handleRequest(httpRequest);
});
});
A HTTP GET request to http://localhost/food/pizza/tomato will output:
HTTP GET
maincourse=pizza
ingredient=tomato
WebSocket STOMP server #
import "package:socks/server_socks.dart" as server;
import "package:socks/shared_socks.dart" as shared;
// Create STOMP destination responding to messages targeting the "kitchen" destination.
class MyDestination extends server.StompDestination {
MyDestination() : super("kitchen");
Future onMessage(String transaction, shared.StompMessage stompMessage) {
print("Received message: ${stompMessage.message}");
return new Future.value();
}
}
// Create WebSocket STOMP request handler.
@server.UriPath("/stomp")
class MyWebSocketStompHandler extends server.StompRequestHandler {
MyWebSocketStompHandler({int maxFrameHeaders, int maxHeaderLen, int maxBodyLen}) : super(maxFrameHeaders: maxFrameHeaders, maxHeaderLen: maxHeaderLen, maxBodyLen: maxBodyLen) {
addDestination(new MyDestination());
}
}
// Configure server in its simplest form
server.Router router = new server.Router();
router.addRequestHandler(new MyWebSocketStompHandler());
new server.Server().bind()
.then((server.Server server) {
server.listen((HttpRequest httpRequest) {
router.handleRequest(httpRequest);
});
});
WebSocket STOMP client #
import "package:socks/client_socks.dart" as client;
// Configure client in its simplest form
client.WebSocketStompConnection.connect("ws://localhost/stomp", "localhost")
.then((client.WebSocketStompConnection connection) {
// Connected. Send a message to all subscribers to the "kitchen" destination.
connection.send("kitchen", "cheese");
});
Notes #
Server instances can support any combination of RESTful, WebSocket, and STOMP WebSocket services by adding corresponding request handlers to the Router instance as shown in the above examples.