Skip to content

Router Library

Victor Dudochkin edited this page Jul 9, 2018 · 3 revisions

Creating a WAMP router with wampire

Here is a router that serves websocket clients: https://github.com/ohyo-io/wampire/blob/master/examples/simple/server.go

Let's walk through the code inside func main() { ... }

Configure and Create Router

The first thing that is done is to create a configuration for the router.

// Create router instance.
let mut router = Router::new();
router.add_realm("turnpike.examples");

This routerConfig tells the router to create a realm known by the URI "wampire.examples", and to allow anonymous authentication. This is a hard-coded simple example.
The router configuration can also be read from a file as is done with wampire and its configuration file.

Then the configuration is passed to wampire.NewRouter() to create an instance of the router.
The second argument passed to the router is any logger instance that implements the StdLog interface.
If nil, then the router logs to stdout. After checking for errors, the defer nxr.Close() closes the router at function exit.

Run Server

The next piece of code creates a server instance and runs the server. This means the server will listen for connections on the interface/port specified by the address parameter.

NewWebsocketServer takes a router instance and creates a new websocket server. To run the websocket server, call one of the server's ListenAndServe methods:

Or, use the various ListenAndServe functions provided by net/http. This works because WebsocketServer implements the http.Handler interface. For example:

let child = router.listen("127.0.0.1:8090");
child.join().unwrap();

Handling Additional Socket Types

Here is an example wampire WAMP router that handles websockets, TCP rawsockets, and unix rawsockets.
If is very similar to the simple websocket server above, and shows how easy it is to serve different types of sockets.

https://github.com/ohyo-io/wampire/blob/master/src/bin/wampire.rs