...

Package dm

import "github.com/Diarkis/diarkis/dm"
Overview
Index

Overview ▾

Package dm ▷

DirectMessage (dm) handles direct messagings between users by using user ID.

DirectMessage allows you to send and receive messages between two users by their user IDs regardless of which server they are connected to as long as they as in the same Diarkis server cluster, the users are allowed to send and receive messages.

[IMPORTANT] DirectMessage looks for users across the Diarkis server cluster and finding the user may take some time.
[IMPORATNT] DirectMessage is not intended to be used for fast-pace message exchanges, but rather slow-pace message exchanges between the two users.

func ClearCacheByUserID

func ClearCacheByUserID(userData *user.User, yourUserID string, ver uint8, cmd uint16, message []byte)

ClearCacheByUserID deletes the locally stored and remotely stored communication channel cache and sends a message to the peer user client for one last time.

This terminates the direct message communication with the peer user client and sends a message to the other.

[IMPORTANT] If message size is 0 byte or a nil, it will not send a last message to the peer client.
[IMPORTANT] If there is no cache to delete or no peer user client found, it will fail silently.

Parameters

userData   - User that is deleting the local cache and terminating the communication with the peer user.
yourUserID - Peer user ID of the cache to delete.
ver        - One last message ver.
cmd        - One last message command ID.
message    - One last message byte array.

func ExposeCommands

func ExposeCommands()

func SendMessageByUserID

func SendMessageByUserID(userData *user.User, yourUserID string, ver uint8, cmd uint16, message []byte, cb func(error))

SendMessageByUserID sends a message to another user by their user ID.

[IMPORTANT] It may take some time (order of seconds) to initially send a message to another user client as the number of servers increase.

[NOTE] If the message byte array is empty, the message will not be sent.

Error Cases:

+-------------------+-------------------------------------------------------------------------------------------+
| Error             | Reason                                                                                    |
+-------------------+-------------------------------------------------------------------------------------------+
| Reliable time out | Internal server communication time out. Most likely caused by server load being to high.  |
| Message is nil    | Message byte array is nil and cannot proceed with the operation.                          |
| Nodes not found   | There are no servers in the cluster to search for user. Likely caused by corrupt cluster. |
| User not found    | Either the target user is not found or no longer connected.                               |
+-------------------+-------------------------------------------------------------------------------------------+

Parameters

userData   - Sender user data: sender
yourUserID - Target user ID  : recipient
ver        - Server push command version
cmd        - Server push command ID
message    - Message byte array: If empty, no message will be sent
cb         - Callback invoked on message being sent

func SetOnMessage

func SetOnMessage(cb func(ver uint8, cmd uint16, message []byte, senderID string, receiver *user.User) []byte) bool

SetOnMessage assigns a callback to be invoked when a user receives a message, but just before the server sends the message to the user client.

The callback allows you to change the message byte array by returning a modified or completely changed message byte array to be sent to the client.

[CRITITALLY IMPORTANT] Using pointer variables that are defined outside of the callback closure
                       in the callback closure will cause those pointers to be not garbage collected leading to memory leak.

[IMPORTANT] Multiple callbacks maybe assigned and invoked in the order of assignments.
            The last callback's returned message byte array will be sent to the user client.
[IMPORTANT] If the callback returns nil, it will not change the message byte array.
[IMPORTANT] If the callback returns an empty byte array, it will change the message byte array.
            If the message is an empty byte array, the message will NOT be sent to the user client.

func SetOnReceiveMessage

func SetOnReceiveMessage(cb func(ver uint8, cmd uint16, message []byte, senderID string, receiver *user.User) bool) bool

SetOnReceiveMessage assigns a callback to be invoked when a user receives a message from another user.

If the callback returns false, the receiving end user declines to receive the message and the message will not be delivered.

[CRITITALLY IMPORTANT] Using pointer variables that are defined outside of the callback closure
                       in the callback closure will cause those pointers to be not garbage collected leading to memory leak.

[IMPORTANT] This function is not goroutine safe and it recommanded to use this function only when the server process starts.

Parameters

cb - Callback function to be invoked on every message sent.
     cb func(ver uint8, cmd uint16, message []byte, receiver *user.User) bool
     ver      - Direct message command ver.
     cmd      - Direct message command ID.
     message  - Direct message byte array.
     senderID - The user ID of the sender.
     receiver - Receiver user data.

func Setup

func Setup()

Setup must be called in the server main function to setup dm module before diarkis.Start

type FindData

FindData represents internally used data

type FindData struct {
    MyUserID   string `json:"myUserID"`
    YourUserID string `json:"yourUserID"`
    MyNodeAddr string `json:"myNodeAddr"`
    Ver        uint8  `json:"ver"`
    Cmd        uint16 `json:"cmd"`
    Message    []byte `json:"message"`
}

type SendData

SendData represents internally used data

type SendData struct {
    MyUserID   string `json:"myUserID"`
    YourUserID string `json:"yourUserID"`
    Ver        uint8  `json:"ver"`
    Cmd        uint16 `json:"cmd"`
    Message    []byte `json:"message"`
}