@nikoladsp ,
You can do that.
server
address in swagger-ui and that all your services are accessible at that host-name void* StartThread(void* arg);
class WSInstanceListener : public oatpp::websocket::ConnectionHandler::SocketInstanceListener {
public:
.....
public:
pthread_t m_pid;
bool m_bQuitThread;
oatpp::websocket::WebSocket* m_Socket;
};
WSInstanceListener::WSInstanceListener() {
m_pid = -1;
m_bQuitThread = true;
m_Socket = NULL;
}
void WSInstanceListener::onAfterCreate(
const oatpp::websocket::WebSocket &socket,
const std::shared_ptr<const ParameterMap> ¶ms) {
SOCKETS++;
OATPP_LOGD(TAG, "New Incoming Connection. Connection count=%d",
SOCKETS.load());
socket.setListener(std::make_shared<WSListener>());
m_Socket = (oatpp::websocket::WebSocket*) &socket;
pthread_create(&m_pid, NULL, StartThread, (void*) this);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_detach(m_pid);
}
void WSInstanceListener::onBeforeDestroy(
const oatpp::websocket::WebSocket &socket) {
SOCKETS--;
OATPP_LOGD(TAG, "Connection closed. Connection count=%d", SOCKETS.load());
m_bQuitThread = false;
pthread_cancel(m_pid);
pthread_join(m_pid,NULL);
this->m_Socket->stopListening();
}
void* StartThread(void *arg) {
WSInstanceListener *lis = (WSInstanceListener*) arg;
lis->m_bQuitThread = true;
while (lis->m_bQuitThread) {
pthread_testcancel();
lis->m_Socket->sendOneFrameText("hello");
}
return (void*) 0;
}
I |2021-12-15 19:46:53 1639568813189617| MyApp:Server running on port 8000
D |2021-12-15 19:47:00 1639568820630942| Server_WSInstanceListener:New Incoming Connection. Connection count=1
D |2021-12-15 19:47:06 1639568826556318| Server_WSListener:onClose code=1001
D |2021-12-15 19:47:06 1639568826556353| Server_WSInstanceListener:Connection closed. Connection count=0
D |2021-12-15 19:47:16 1639568836406451| Server_WSInstanceListener:New Incoming Connection. Connection count=1
D |2021-12-15 19:47:21 1639568841210745| Server_WSListener:onClose code=1001
terminate called after throwing an instance of 'std::runtime_error'
D |2021-12-15 19:47:21 1639568841210782| Server_WSInstanceListener:Connection closed. Connection count=0
terminate called recursively
Hello.
I've got an annoying issue I'm trying to fix on my oat++ client application : if my server shuts down before it gets the time to send a response to my client after a client request, it causes my client to crash. This is probably a common problem. Any way to fix this client side ?
Hey @nikoladsp ,
In an ideal world you would have to decouple the DB model and REST DTOs. The way you present data to service users and the way you store data may differ (and they do differ in most cases). Also, it's not recommended to expose DB ids - since they do not describe the actual resource in most cases.
Treat CRUD service as an adaptor of REST interface to DB model.
So the answer is - create two sets of DTOs - one for REST, second for DB.
Read from DB - do whatever transformations/compositions you need - join data in memory if needed - form response DTO and return it on the endpoint.
Hey @mmn:matrix.org ,
if the WebSocket connection was closed gracefully - you'll receive the onClose
method call in WS listener first, then the listen method will return.
If there was any kind of network issue - listen method will just return - no onClose
event triggered.
Here is the implementation of listen - https://github.com/oatpp/oatpp-websocket/blob/master/src/oatpp-websocket/WebSocket.cpp#L279
The browser establishes a link with the server websocket and actively sends data. The browser refreshes the page and closes the page for the first time. The server runs normally; The browser establishes a link with the server websocket and actively sends data. When the browser closes the page for the second time, it will send 1001 status code to the server, and the program crashes;
@oatpp_io_twitter Hello, the following is the current code. Is there a better optimization method?
Hey @shijiantouzouyiqie ,
Your program is missing a lot of things - and it has critical bugs:
std::thread
instead of the pthread - just for simplicity and portabilitym_bQuitThread
m_bQuitThread
is set to true sendOneFrameText
call inside of try - catch
block.WebSocket
with my own listen()
(and stopListening()
; can't use m_listening
because it's private) which passes the exceptions through.
oatpp:: websocket:: websocket * m_socket;
used here is initialized in the structure as a member variable. It is understood that it should be initialized bym_socket = new oatpp:: websocket:: websocket();
. If an error is reported, I don't know how to solve this problem....
[oatpp::postgresql::mapping::Deserializer::deserializeString()]: Error. Unknown OID.
I keep fighting some odd linker errors about missing vtable/constructors for DTO files even though they are correctly defined when linking across a few projects. But since the DTO's are only headers the static library for the DTO's are 8 bytes and basically empty.
I link everywhere else but on windows I get errors about the Vtable for the API-Client DTOs
I suspect the client-dto-lib is actually empty and brining no extra symbols over, but the dtos are reachable at compile time so I only get a link error. Still somewhat confused how
@Maboulmagd
I was wondering, how often do websockets disconnect? i.e Is it once every 10 hours, or does it depend on the host that I am listening to and their rules?
Yes, It depends on the host rules and on the internet connection.
If you want your events stream to be stable - you should have a reconnect functionality.
Also, is there an implementation of the reconnecting functionality, that I can take a look at? Or is it as simple as using the connector to reconnect to the endpoint I am interested in, and resubscribing to streams that I was initially subscribed to?
Yes, it's just as simple as using the connector to connect again.
Hello. It's me again.
I'm trying to shove the content of a std::vector<bool> object inside a List<Boolean> DTO field, but the compiler isn't very happy about this and tells me there is no matching member function to call puch_back(), which is kinda strange because I managed to shove a std::vector<std::string> inside a List<String> DTO field without trouble.
What's the issue here ?
auto dtoEndosInfos = EndoResponse::createShared();
dtoEndosInfos->nbFiles = vecInfos.nbFiles;
for (int i = 0; i < vecInfos.nbFiles; i++)
{
dtoEndosInfos->name->push_back(vecInfos.name[i]);
dtoEndosInfos->isStamp->push_back(vecInfos.isStamp[i]);
dtoEndosInfos->block->push_back(vecInfos.block[i]);
}
class EndoResponse : public oatpp::DTO
{
DTO_INIT(EndoResponse, DTO)
DTO_FIELD(List<String>, name);
DTO_FIELD(List<Boolean>, isStamp);
DTO_FIELD(List<String>, block);
DTO_FIELD(Int32, nbFiles);
};