|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.streamhub.nio.NIOServer
public class NIOServer
The main Comet and HTTP Push server to which connections are made by all clients.
A common idiom for running the server until it is stopped by pressing a key is:
PushServer server = new NIOServer(80);
server.start();
System.out.println("Server started on port 80");
System.out.println("Press any key to stop...");
System.in.read();
server.stop();
System.out.println("Server stopped");
In order to stream data to clients, listeners need
to be added to the SubscriptionManager.
PushServer server = new NIOServer(80); server.start(); SubscriptionManager subscriptionManager = server.getSubscriptionManager(); subscriptionManager.addSubscriptionListener(this); subscriptionManager.addPublishListener(this);
For more details on getting started please refer to the guides below:
SecureNIOServer| Constructor Summary | |
|---|---|
NIOServer(InetSocketAddress address)
Creates a new NIOServer bound to address. |
|
NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress)
Creates a new NIOServer bound to address and a streaming adapter bound to streamingAdapterAddress |
|
NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress,
String licenseDir)
Creates a new NIOServer bound to address and a streaming adapter bound to streamingAdapterAddress. |
|
NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress,
URL licenseUrl,
URL log4jConfigurationUrl)
Creates a new NIOServer bound to address and a streaming adapter bound to streamingAdapterAddress, loading the license and log4j xml configuration from the alternative locations specified by licenseUrl
and log4jConfigurationUrl respectively. |
|
NIOServer(int port)
Creates a new NIOServer bound to the wildcard address |
|
| Method Summary | |
|---|---|
void |
addContext(String context,
com.streamhub.handler.Handler handler)
Adds or overrides a context. |
void |
addStaticContent(File directory)
Adds a directory to be served from the root context / as if it were
a standard HTTP server. |
void |
addStaticContent(File directory,
String context)
Adds a directory to be served from the a specified context as if it were a standard HTTP server. |
SubscriptionManager |
getSubscriptionManager()
Returns the servers SubscriptionManager. |
boolean |
isStarted()
Identifies whether the server is currently started or not |
static void |
main(String[] args)
|
void |
publish(String topic,
Payload payload)
Sends the payload to all clients who are subscribed to topic. |
void |
setDefaultHeader(String name,
String value)
Sets the value of a default HTTP header for synchronous responses. |
void |
setDefaultPushHeader(String name,
String value)
Sets the value of a default HTTP header for asynchronous push responses. |
void |
start()
Starts the server |
void |
stop()
Stops the server |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public NIOServer(int port)
port - the port to listen onpublic NIOServer(InetSocketAddress address)
address - the address to bind to
public NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress)
address - the address to bind the server tostreamingAdapterAddress - the address to listen for streaming adapters
public NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress,
String licenseDir)
null as the second argument.
address - the address to bind the server tostreamingAdapterAddress - the address to listen for streaming adapters or null if
a streaming adapter is not requiredlicenseDir - an alternative directory to load the license from. The
license file must be called "license.txt"
public NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress,
URL licenseUrl,
URL log4jConfigurationUrl)
licenseUrl
and log4jConfigurationUrl respectively. To choose not to start a streaming
adapter, pass null as the second argument.
The following URL formats are supported for the licenseUrl and
log4jConfigurationUrl parameters:
file:///C:/licenses/license.txthttp://www.example.com/logging/log4j.xmljar:file:///C:/lib/streamhub-license.jar!/license.txt or jar:http://www.example.com/logging/streamhub-logging.jar!/log4j.xmlcom.streamhub.util.UrlLoader e.g. UrlLoader.load("classpath:/license.txt"), or UrlLoader.load("classpath:/conf/log4j.xml")The default location for the license file is a file named "license.txt" in the current working directory (or "."). The default location for the log4j configuration is "conf/log4j.xml" relative to the current working directory.
address - the address to bind the server tostreamingAdapterAddress - the address to listen for streaming adapters or null if
a streaming adapter is not requiredlicenseUrl - an alternative URL to load the license fromlog4jConfigurationUrl - an alternative URL to load the log4j xml configuration from| Method Detail |
|---|
public static void main(String[] args)
throws Exception
Exception
public void stop()
throws IOException
PushServerThis method will block until all the connections have been forcibly closed and internal threads shutdown
stop in interface PushServerIOExceptionpublic void start()
PushServerNote this method is non-blocking
start in interface PushServerpublic SubscriptionManager getSubscriptionManager()
PushServerSubscriptionManager.
The SubscriptionManager provides the core publish and subscribe
listening capabilities of the server.
Use SubscriptionManager.addPublishListener(PublishListener) to
receive callbacks when a client publishes a message to the server.
Use SubscriptionManager.addSubscriptionListener(SubscriptionListener) to
receive callbacks when a client subscribes to a topic.
It is recommended to call this method after starting the server using PushServer.start()
getSubscriptionManager in interface PushServerSubscriptionManager associated with this serverpublic boolean isStarted()
PushServer
isStarted in interface PushServertrue if the server is currently started and running,
false if it has not been started or has been stopped
public void publish(String topic,
Payload payload)
PublisherNIOServer.
This method should generally be preferred over Client.send(String, Payload)
except where unique information must be sent to each client.
StreamHub keeps its own internal map of subscriptions. If you need
to send something on a topic which a client has not subscribed to, use
Client.send(String, Payload).
publish in interface Publishertopic - the topic on which to send the messagepayload - the message payloadPayloadpublic void addStaticContent(File directory)
PushServer/ as if it were
a standard HTTP server. This allows static content, for example HTML pages,
to be retrieved via a web browser.
Although StreamHub is capable of serving normal static content it was primarily designed as a Comet and HTTP Push server. For a live website or application it is recommended to serve static content from a standard HTTP Web server such as Apache or IIS.
addStaticContent in interface PushServerdirectory - the directory on the local filesytem to make available via the webPushServer.addStaticContent(File, String)
public void addStaticContent(File directory,
String context)
PushServerAlthough StreamHub is capable of serving normal static content it was primarily designed as a Comet and HTTP Push server. For a live website or application it is recommended to serve static content from a standard HTTP Web server such as Apache or IIS.
addStaticContent in interface PushServerdirectory - the directory on the local filesytem to make available via the webcontext - the context to serve the directory under. For example using a context
of site would make content available under
http://serverurl/site/PushServer.addStaticContent(File)
public void setDefaultPushHeader(String name,
String value)
PushServer
setDefaultPushHeader in interface PushServername - the name of the headervalue - the value of the header
public void setDefaultHeader(String name,
String value)
PushServer
setDefaultHeader in interface PushServername - the name of the headervalue - the value of the header
public void addContext(String context,
com.streamhub.handler.Handler handler)
PushServer/examples/*.
Any incoming requests which match the context will be routed to the handler.
addContext in interface PushServercontext - a context specifier string e.g. /examples/*handler - a handler which will handle requests matching the context
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||