I have been using signal R to send simple chat messages. Now I have a requirement to pass complex object.
chatEventHubProxy.invoke('sendGroupMessage', jsonObject, recipients, message, chatType).done(function() {
self.chatEventLog('Chat message send success to ' + recipients);
}).fail(function(error) {
console.log('Chat message send failure reason ' + error);
})
This is a workaround that I have used. I am using object in the below method just for the sake of POC. I would change it to its respective Model.
From Client to Server,
public void sendGroupMessage(object jsonObject, string msgTo, string msgBody, int chatCategory)
{
string[] recipients = msgTo.Split(',');
foreach (string recipient in recipients)
{
XmppClient xmppClient = XmppClients[Context.ConnectionId];
var msg = new Matrix.Xmpp.Client.Message(recipient, MessageType.Chat, body: msgBody, subject: jsonObject.ToString(), thread: chatCategory.ToString());
xmppClient.Send(msg);
}
}
From Server to Client,
private void xmppClient_OnMessage(object sender, MessageEventArgs e)
{
DisplayEvent("OnMessage");
var message = new Message { From = e.Message.From, Body = e.Message.Body, Subject = e.Message.Subject, ChatCategory = e.Message.Thread };
if (e.Message.XDelay != null)
{
message.Stamp = e.Message.XDelay.Stamp;
}
if (e.Message.Body != null)
{
Clients.Client(Context.ConnectionId).onMessage(message);
}
}
I know this is not the right way to do. This is just a workaround. But this is working as of now to some extent.
I want to know if there is any way to pass custom object instead of sending new Matrix.Xmpp.Client.Message object as a parameter in xmppClient.Send() Or is there any way to pass the object in Matrix.Xmpp.Client.Message(not as a string).
I am looping the recipients and sending message for a group chat. Is there any other way to do? I tried by passing groupname and changing message type as GroupChat. But still it did not work.
Any help would be greatly appreciated.
As a client in XMPP you send only stanzas of type or The XMPP protocol and MatriX are extensible. So can add any type of payload (custom object) and children to the top level stanzas types I mentioned above.
msg.Add(myCustomType)
Xml sample:
<message to='foo@bar.com' type='chat'>
<x xmlns='my-custom namespace'>
<foo/>
</x>
</message>
See also the Extending MatriX section in the Tutorial.