socks 0.1.1 copy "socks: ^0.1.1" to clipboard
socks: ^0.1.1 copied to clipboard

outdatedDart 1 only

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.

Sample code #

The following sample code is intended to show common usages in their simplest form. Please consult the source code documentation of the referenced functions to support more complex requirements.

HTTP static file server #

server.Router router = new server.Router();
router.addRequestHandler(new server.StaticHttpRequestHandler("../web"));
new server.Server().bind()
.then((server.Server server) {
    server.listen((HttpRequest httpRequest) {
        router.handleRequest(httpRequest);
    });
});

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("Main 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:

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 "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.

0
likes
0
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

Client and server package for building and consuming HTTP/1.1 web services including RESTful services, WebSocket services, and STOMP 1.2 WebSocket services.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

logging, mime_type

More

Packages that depend on socks