StreamServer constructor
StreamServer({})
Constructor. *
- ##Request Handlers
- A request handler is responsible for handling a request. It is mapped
- to particular URI patterns with
uriMapping
. - The first argument of a handler must be HttpConnect. It can have optional
- arguments. If it renders the response, it doesn't need to return anything
- (i.e.,
void
). If not, it shall return an URI (which is a non-empty string, - starting with *
/
) that the request shall be forwarded to. - ##WebSocket Handling
- To handle WebSockets, you can prefix the URI mapping with
'ws:'
, - and then implement a WebSocket handler. A WebSocket handler has a
- single argument and the argument type must be WebSocket. For example,
-
new StreamServer(uriMapping: {
-
"ws:/foo": (WebSocket socket) {
-
socket.listen((event) {
-
//event is the message sent by the client
-
//you can handle it and return a message by use of socket.add()
-
});
-
return socket.done;
-
},
-
}).start();
- Note: The
ws:
prefix in the mapping table maps both "ws://" and "wss://". - ##Arguments
-
homeDir
- the home directory for holding static resources. If not specified,
- it is the root directory of the application.
- You can specify a relative path relative to the root
- directory. For example, you can create a directory called
static
to hold - the static resources, and then specify
static
as the home directory. -
> The root directory is assumed to the parent directory of the `webapp`
-
> directory if it exists. Otherwise, it is assumed to be the directory where
-
> the `main` Dart file is. For example, if you execute `dart /foo1/myapp.dart`,
-
> the root is assumed to be `/foo1`. On the other hand, if executing
-
> `dart /foo2/webapp/myapp.dart`, then the root is `/foo2`.
-
uriMapping
- a map of URI mappings,<String uri, RequestHandler handler>
- or
<String uri, String forwardURI>
. - The key is a regular expression used to match the request URI. If you can name
- a group by prefix with a name, such as
'/dead-link(info:.*)'
. - The value can be the handler for handling the request, or another URI that this request
- will be forwarded to. If the value is a URI and the key has named groups, the URI can
- refer to the group with
(the_group_name)
. - For example:
'/dead-link(info:.*)': '/new-link(info)'
. -
filterMapping
- a map of filter mapping,<String uri, RequestFilter filter>
.
- The key is a regular expression used to match the request URI.
- The signature of a filter is
void foo(HttpConnect connect, void chain(HttpConnect conn))
. -
errorMapping
- a map of error mapping. The key can be a number representing
- the status code, e.g., 404 and 403.
- The value can be an URI or a renderer function. The number is used to represent a status code,
- such as 404 and 500.
- Trick: You can use a special number to indicate a special case.
- For example, in our sample app (features), we use -900 to denote
- an exception that can be recovered.
- Trick: You can also override (DefaultRouter.getErrorHandler) to
- detect the type and return a proper handle it for it.
-
disableLog
- whether to disable logs.
- If false (default),
Logger.root
will be set toLevel.INFO
, and - a listener will be added logger.
Implementation
factory StreamServer({Map<String, dynamic>? uriMapping,
Map<int, dynamic>? errorMapping, Map<String, RequestFilter>? filterMapping,
String? homeDir, bool disableLog = false})
=> new _StreamServer(
new DefaultRouter(uriMapping: uriMapping,
errorMapping: errorMapping, filterMapping: filterMapping),
homeDir, disableLog);