How to listen to an event using sockets?

二次信任 提交于 2021-01-29 06:26:49

问题


We are trying to add play/pause/record buttons to a webpage that shows a video stream.

The video is streaming from the server and the webpage is on the client-side.

Now the client can see the video stream on the webpage, but we wanted to add some buttons. This is the code that sends the video using sockets:

void stream(cv::Mat* frame){
    TCPServer serverInputConnection("localhost", 8080);
    serverInputConnection.createListener();

    ////////////////////// reconnect network cycle //////////////////////
    while(1)
    {
        if(serverInputConnection.waitConnect() == TCPServer::STATE_ERR)
        {
            cout << "reconnect...\n" << endl;
            sleep(2);
            continue;
        }
        // read HTTP header
        serverInputConnection.readMsg();
        cout << "Client header:\n" << serverInputConnection.inMsg.data() << endl; // show http header
        serverInputConnection.inMsg.clear();

        cout << "start stream" << endl;

        // send http header
        string headImg;
        headImg += "HTTP/1.0 200 OK\r\n";
        headImg += "Cache-Control: no-cache\r\n";
        headImg += "Pragma: no-cache\r\n";
        headImg += "Connection: close\r\n";
        headImg += "Content-Type: multipart/x-mixed-replace; boundary=jpegboundary\r\n\r\n";

        while(1){
            vector<unsigned char> jpgBuff;
            jpgBuff.clear();
            std::vector<int> params;
            params.push_back(CV_IMWRITE_JPEG_QUALITY);
            params.push_back(50);

            imencode(".jpg", *frame, jpgBuff, params);

            // send http separation
            headImg += "--jpegboundary\r\n";
            headImg += "Content-type: image/jpeg\r\n";
            headImg += "Content-length: " + to_string(jpgBuff.size()) + "\r\n\r\n";
            std::vector<char> strBuff(headImg.begin(), headImg.end());


            headImg, serverInputConnection.Msg = strBuff;
            if(serverInputConnection.sendMsg()!= TCPServer::STATE_OK)
            {
                break;
            }

            headImg.clear();

            // send image
            if(serverInputConnection.sendMsg(jpgBuff) != TCPServer::STATE_OK)
            {
                break;
            }
        }
    }
    serverInputConnection.disconnect();
}

and this is the code for the TCPServer functions:

TCPServer::status TCPServer::createListener()
{
    isConnect = false;

    // create socket
    sockIn = ::socket(AF_INET, SOCK_STREAM, 0);
    if(sockIn < 0)
    {
        cout << "ERROR opening socket\n" << endl;
        return currentState = STATE_ERR;
    }

    int yes=1;
    //char yes='1'; // use this under Solaris
    if (setsockopt(sockIn, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
    {
        cout << "setsockopt\n" << endl;
        return currentState = STATE_ERR;
    }
    if (setsockopt(sockIn, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) == -1)
    {
        cout << "setsockopt\n" << endl;
        return currentState = STATE_ERR;
    }

    bzero(&serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY; //any incoming messages
    serv_addr.sin_port = htons(localPort); // is old function

    //serv_addr.sin_port =  bswap_16(localPort);

    // select listen port
    int bindRes = bind(sockIn, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr_in));
    if (bindRes < 0)
    {
        cout << "ERROR on binding\n" << endl;
        return currentState = STATE_ERR;
    }

    cout << "listen input port: " << localPort << endl;

    if(listen(sockIn, 5) != 0)
    {
        cout << "ERROR on listen socket\n" << endl;
        return currentState = STATE_ERR;
    }

    return currentState = STATE_OK;
}

Questions:

  1. How can we know when the user clicks a button? (e.g. if the user clicks on pause we want to stop the streaming).
  2. What is the best way to do this? (cgi, socket, PHP, another way)

来源:https://stackoverflow.com/questions/65811174/how-to-listen-to-an-event-using-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!