Interface MessageService
public interface MessageService
Service for asynchronous communication between clients. All messages
are sent over "channels" identified by a unique string ID, so that
clients can send and retrieve only the messages they are interested
in. Within each channel it is also possible to make sure a message
only reaches a specific user.
The service also keeps track of the users currently logged in to
the server where the service is running.
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener for changes to the service, such as users logging on/off.voiddispose()Disposes the message service after it is no longer to be used.Gets a logged-on user by username.getUsers()Returns the currently logged on users.default booleanReturns true if the connection to the server is lost.booleanisOpen()Returns true if the service is open and able to send and receive messages.me()Returns the current user for the service.voidPublishes a string message on a channel.voidpublish(String channelID, String message, MTUserHeader receiver) Publishes a string message on a channel, but only to a given user.voidPublishes a message on a channel.voidPublishes a message on a channel, but only to a given user.booleanTries to reconnect after loosing connection with the server.voidRemoves a listener for changes to the service.voidsubscribe(String channelID, MessageListener l) Subscribes to messages sent on a channel.voidSubscribes to the server callback channel.voidunsubscribe(String channelID, MessageListener l) Unsubscribes a listener from messages on a channel.void
-
Method Details
-
isOpen
boolean isOpen()Returns true if the service is open and able to send and receive messages.- Returns:
- true if the service is open and able to send and receive messages.
-
subscribe
Subscribes to messages sent on a channel.- Parameters:
channelID- The ID of the channel.l- The listener to receive new messages.
-
unsubscribe
Unsubscribes a listener from messages on a channel.- Parameters:
channelID- The ID of the channel.l- The listener to remove.
-
subscribeToServerCalls
Subscribes to the server callback channel. This channel is only used to let the server call clients to inform, warn or report an error.- Parameters:
l- Consumer of the server message. The message is json and always contains- a string property "category" with the valid values "info", "warn" and "error"
- a string property "domain" denoting the type, or "domain" of the warning/error/info
- a property "message" which can be any json-object
{ "domain" : "server.load", "category" : "info", "message": "server.load.ok" } or { "domain" : "mail.notification", "category" : "warn", "message": { "code" : "mail.notification.error", "unsent" : ["bill@domain.com", "alice@florb.com"] } }
-
unsubscribeFromServerCalls
-
publish
Publishes a message on a channel.- Parameters:
channelID- The ID of the channel.message- The message to send.
-
publish
Publishes a message on a channel, but only to a given user.- Parameters:
channelID- The ID of the channel.message- The message to send.receiver- The only user that will receive the event, given that this user actually have subscribed to this channel.
-
publish
Publishes a string message on a channel.- Parameters:
channelID- The ID of the channel.message- The message to send.
-
publish
Publishes a string message on a channel, but only to a given user.- Parameters:
channelID- The ID of the channel.message- The message to send.receiver- The only user that will receive the event, given that this user actually have subscribed to this channel.
-
addMessageServiceListener
Adds a listener for changes to the service, such as users logging on/off.- Parameters:
l- The listener to add.
-
removeMessageServiceListener
Removes a listener for changes to the service.- Parameters:
l- The listener to remove.
-
getUsers
List<MTUserHeader> getUsers()Returns the currently logged on users.- Returns:
- the currently logged on users.
-
getUser
Gets a logged-on user by username.- Parameters:
username- The username of the user to return.- Returns:
- The user with the given username.
-
me
MTUserHeader me()Returns the current user for the service.- Returns:
- the current user for the service.
-
reconnect
boolean reconnect()Tries to reconnect after loosing connection with the server.- Returns:
- true if the service was able to reconnect, false otherwise.
-
dispose
void dispose()Disposes the message service after it is no longer to be used. -
isConnectionLost
default boolean isConnectionLost()Returns true if the connection to the server is lost.- Returns:
- true if the connection to the server is lost.
-