Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebSockets Sending messages with escaped characters and QLineEdit Inputs



  • Hi all, I'm currently developing a client that connects to a server via websockets with a message protocol that requires the use of escaped characters such as \n and \r, and also quotations (""). As I'm using QT, I decided to use QtWebSockets, but I'm having two issues, those being the quotations and the escape characters. I can send a message such as CONNECT { "name": "ollie" } to the server, but this would be first stored as a QString before using WebSocketVariable.sendTextMessage(QStringVariable). To do this, I would need to escape the quotations such that CONNECT { "name": "ollie" } becomes Qstring val = "CONNECT { \"name\": \"ollie\" }";. This, however, when being sent to the server will send with the backslashes, which it can't handle.

    The other issue, along similar lines, is \r and \n (\r\n is used to represent the end of a message). The problem is these are input from an input box (QLineEdit) into a QString and become \\r and \\n, which does not work for the server. I've come up with a hack of replacing the \r with \r and \n with \n, But I'd rather it just didn't change the string input.

    Any ideas how I can go about solving these issues?



  • @Ollie_A
    I don't know this makes much sense if you don't show your actual code. For example, you state

    Qstring val = "CONNECT { \"name\": \"ollie\" }";. This, however, when being sent to the server will send with the backslashes, which it can't handle.

    No, it won't send any backslashes.

    There should be no issues pertaining to QWebSockets messages/QLineEdit inputs here.

    I wonder if your confusion is more to do with C++ literals, or similar.

    In any case, in view of your question please, please use backticks in your posts of code here. Single backtick at start and end of code if in-line, tripe backticks on line of their own before first line and after last line if code on its own lines....

    This is in-line code.

    This is standalone
    possibly multiline code
    


  • Hi Jon, Sorry for the late reply, I didn't see any come through; Yes I believe my confusion was to do with C++ literals.

    Thanks for the info on backticks to show code, I'll make sure to use that in the future when asking more questions!


Log in to reply