Qt: How to send a SQLTableModel from server to client - c++

I am working on a client-server project. On the client side, I have a few GUI windows containing different views (QTableView, QListView). I can send messages to the server just fine using the following method:
QByteArray bArray;
QDataStream out(&bArray, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
//....//
out << quint16(0) << message;
out.device()->seek(0)
out.quint16(bArray.size() - sizeof(quint16));
socket.write(block);
My problem however comes on my server side. My server will receive the message, go through a couple of steps reading the message to decide the necessary info to get from the SQL database, run the Query returning a Model(QSqlTableModel, QSqlQueryModel), but should then send the respective model back to the client.
Is there any way for me to send this model back to the server?

Related

C++/TCP Server - Convenient way of event notification

I have created a simple C++ TCP Server application.
Client connects and receives back as a simple echo everything that the client sends to the server. No purpose at all except for me to test the communication.
So far so good. What comes as next task for me is to decide a way of how to send a notification to the server that specific event has started.
Some event examples:
Player wrote a message - Server accepts the data sent from the client and recognizes that it's a chat message and sends back data to all connected clients that there is new message. Clients recognize that there is new message incoming.
Player is casting spell.
Player has died
Many more examples but you get the main idea.
I was thinking of sending all the data in json format and there all messages will contain identifiers like
0x01 is message event.
0x02 is casting spell event.
0x03 is player dead event.
And once identifier is send server can recognize what event the client is asking/informing and can apply the needed logic behind.
My question is isn't there a better approach to identify for what event the server is notified ?
I am in a search of better approach before I take this road.
You can take a look at standard iso8583 message, it's a financial message but every message has a processing code that determine what action should be done for each incoming message.

Qt5 storing list of websocket clients

I am trying to store a list of connected QWebSockets in a QHash for a subscription purpose.
A client connects and then sends the websocket server a feature they would like to subscribe to. Each feature set has a QHash that they get added to. When the feature has new information, it loops through the clients in the QHash and sends them a message.
That seems to work fine, but if a client disconnects and then the feature tries to send them a message my application seg faults. On a client disconnect I am trying to remove the client from the QHash but the Qhash::remove function always returns 0.
The way I'm storing Websocket clients is with a pointer to the QWebSocket object as a hash:
QHash<QWebSocket *, QString> diagnosticSubscriptions;
When a client subscribes to the diagnostics feature in my textMessageReceived function I run:
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
diagnosticSubscriptions.insert(pClient, callback);
Then when the user disconnects I try to clean up the QHash:
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
diagnosticSubscriptions.remove(pClient);
pClient->deleteLater();
If I print the result of remove it is 0 so the client isn't being removed.
Is there a better way of storing QWebSockets?

How to redirect messages from one endpoint to another using java websockets (jsr 356)

I am doing a personal project to learn about java websockets. Basically my HTML clients sends messages to my server through JSR 356 websockets. The endpoint #ServerEndpoint("/wsServer") receives all messages from the clients and I would like to see those messages in another portal/endpoint #ServerEndpoint("/wsDashboard") as a dashboard in real time.
I would appreciate any idea, documentation or piece of code to start with as I really have no idea, I have seen solutions like apache kafka, but I am not sure if this is what I need.
thanks
Create Java Web Socket Client for /wsDashboard on onmessage method of a #ServerEndpoint("/wsServer") end point and keep it in user properties. wher you receive a message just forward the message to /wsServer web socket.
#ServerEndpoint("/wsServer")
public class MyWSServerEndPoint{
#OnOpen
public void open( Session session) {
// Open java client websocket connection to /wsDashboard and keep the object in
session.getUserProperties().put("wsDashboard ", websocket);
}
#OnMessage
public void onMessage(String message,Session session){
//forwarding the message
session.getUserProperties().get("wsDashboard ").getRemote().send(message);
}
#OnClose
public void onClose(Session session){
session.getUserProperties().get("wsDashboard ").close();
}
}
To create a client end point here is good example
http://www.hascode.com/2014/11/creating-different-websocket-chat-clients-in-java/

C++, Mongoose: How to make a POST request?

I'm working on a project that uses Mongoose, and I need to make a POST request to another server. I don't see an example of how to do this in their examples list, does anyone know how to do this?
EDIT to add more detail:
I'm working within a larger C++ app and need to create a simple server such that a user can query the app for information. Right now, I start the server like this:
Status sampleCmd::startServer()
{
Status stat = MS::kSuccess;
struct mg_server *server;
// Create and configure the server
server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080");
stopServer = false;
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
while (!stopServer) //for (;;)
{
mg_poll_server(server, 1000);
}
// Cleanup, and free server instance
mg_destroy_server(&server);
return stat;
}
In my event handler, I parse the provided URI for a particular one and then run some commands with the application's API. I need to send these results back to a server for the user to see. It's this latter step that is unclear to me. It seems odd that a web server library wouldn't have some client functionality, don't servers need to talk to other servers?
Okay, it turns out I was thinking about this wrong. I needed to respond to the POST request I was getting. So using mg_printf_data(...) with the connection object worked for me.

Trigger REST adapter to push records to store

I have the following setup:
The ember frontend is connected to a websocket server
The backend pushes records (real-time data) via websocket to the clients, as stringified JSON
The client receives the data, and must now update the store with this new data received
The problem that I have is that I do not know to process the raw JSON data to make it compatible to what is in the store. I can of course parse the json (JSON.parse), but this is just a part of what the REST adapter is doing.
When doing a normal REST request, more or less what happens is that:
server generates reply -> REST adapter converts it -> it gets pushed to the store
But now, since I am not using the REST adapter to process this data (because this is not a request triggered in the client side, but a notification coming from the server side), I do not know how to trigger the normal processing that the REST adapter performs.
How can I trigger the REST adapter programmatically? Can I pass it the stringified JSON coming from the websockets server?
Is it possible to hook the REST adapter to a generic websockets callback, where the only thing I have is the stringified JSON coming from the websockets server?
This is the code that I have (inspired in web2py)
function connect_websocket() {
console.log('connect_websocket > connecting to server');
var callback = function(e) {
var data = JSON.parse(e.data);
console.log('Data received > data=%o', data);
// TODO;
// - process the data as the REST adapter would do, and push new / updated records to the store.
// - handle record deletes too (how?)
};
if(!$.web2py.web2py_websocket('ws://127.0.0.1:8888/realtime/mygroup', callback)) {
alert('html5 websocket not supported by your browser, try Google Chrome');
}
}
I have taken a look at EmberSockets, but as far as I understand that does not offer a generic method of updating records in the store, but just a very specialized way of updating properties in the controllers (which requires a lot of configuration too).
What I am looking for is a generic method of triggering the ember REST adapter from a websockets server. Is there such a thing?