No announcement yet.

CGI and AJAX Long Polling

  • Filter
  • Time
  • Show
Clear All
new posts

  • CGI and AJAX Long Polling

    So a friend and I wanted to write a browser game using AJAX. This game may involve many players being on the same server at once.

    Basically AJAX Long Polling (aka Comet) is sending a request to the CGI application via XMLHttpRequest the CGI application only responds to this request when a change has happened. Currently I have it checking for 30 seconds, if nothing changes it sends a message to browser saying it timed out and browser sends a new request. I only want to update the browser when a change has happened. Specifically in the "zone" that player may be in.

    The issue is lets say each server can have 1000 concurrent players (this is just a number I'm throwing out there, its not necessarily how it will actually be). Each time someone is playing they open a new instance of the CGI executable. Obviously 1000 CGI executables running (I assume) would cause major strain on the server.

    Is there any method to consolidate these into one instance somehow. I am new to this CGI stuff. Thanks.
    Thank you,
    Ryan M. Cross

  • #2
    FastCGI The Forgotten Treasure
    Replace C language with PowerBasic


    • #3
      Very interesting, thanks for the info. Although not looking forward to converting from C.
      Last edited by RyanCross; 17 Jul 2009, 05:05 PM.
      Thank you,
      Ryan M. Cross


      • #4
        In my tests with the Abyss WebServer i have only tried (Local-TCP/IP Sockets)

        From the Abyss Manual
        Note that for most FastCGI compliant interpreters, you can either choose
        FastCGI (Local - Pipes) or FastCGI (Local - TCP/IP Sockets) with no noticeable
        difference as both modes provide comparable performances.

        But some FastCGI interpreters such as PHP 5.1.3 and later only support a single
        mode of operation. So if an interpreter fails to work in a given mode, select
        the other and retry.


        • #5
          Yeah I tried that with their example, but it just hangs there. Before venturing off on my own I was hoping to at least get their example working correctly so I know if there is a problem in the future its most likely me and not the web server.

          Running on Windows XP 32-bit.

          I am trying to run the FCGIecho.exe example from over at I followed their instructions on how to get it working with Abyss.

          Normally I get an Error 500 with the following error code:
          Writing 5173 bytes failed = The process cannot access the file because it is being used by another process.

          The number of bytes seems to change each time. All of them are usually in the 5000s though.

          However, if I sit there and spam refresh on my browser as quickly as a I can and watch Task Manager. Eventually after 4 or 5 seconds of spamming refresh you will see quite a few FCGIecho.exe running. After a second or so all of them will go away except for one or two, which will keep running.

          While these are running everything works fine, of course until they eventually times out with lack of activity.
          Thank you,
          Ryan M. Cross


          • #6

            I downloaded from Coastrd the basic examples, and their libfcgi2.dll.

            Tried to run the ex_echo. It did not make it past Fcgx_Initrequest.

            However, i modified ex_echo to run with the DLL from OpenMarket, and
            it ran succesfully. Still i had to change FCGX_Putstr to FCGX_Puts to
            make it work.

            Also I had to add FCGX_Init at the beginning of the program, which for
            some reason is not present in the original program.

            Another thing, because this program is not multiThreaded, if many
            requests arrive the Abyss Webserver will spawn(shell) additional
            instances of this program. (FCGX_Accept_r not ready)



            • #7
              Is there anyway I can get your ex_echo and can you let me know where to get the OpenMarket DLL?
              Thank you,
              Ryan M. Cross


              • #8

                You need FCGI.DLL and FCGIAPP.INC. Forgot where it was downloaded from.
                But you may get them from, or Aprelium. Maybe in the PB Forums.

                Still, don't know why echo program is not working with Coastrd library.
                Program fails in FGCX_InitRequest.

                With respect to AbyssServer shelling multiple instances of any program.
                It's like running plain CGI. That's why multithreading is needed.

                Modified program
                #COMPILE EXE "FCGIecho.exe" '  
                '#INCLUDE "FCGX_Header.bas" 
                #INCLUDE "FCGIAPP.INC"
                '  Decode all of the special characters in a CGI string
                '  This is copied directly from PB's cgi include file that comes with PBCC
                Function DecodeCGI(ByVal t As String) As String
                LOCAL b_in  As Byte Ptr
                LOCAL b_out As Byte Ptr
                LOCAL h As String Ptr * 2
                LOCAL a As Asciiz Ptr
                    If Len(t) = 0 Then Exit Function
                    b_in  = StrPtr(t)
                    b_out = b_in
                        If @b_in = 43 Then          'convert plus to space
                                @b_out = 32
                        ElseIf @b_in = 37 Then      'process special chars
                                h = b_in + 1
                                @b_out = Val("&H" + @h)
                                b_in = b_in + 2
                                @b_out = @b_in
                        End If
                        Incr b_in
                        Incr b_out
                    Loop Until @b_in = 0
                    @b_out = 0
                    a = StrPtr(t)
                    Function = @a
                End Function
                FUNCTION PBMAIN
                  LOCAL i AS LONG, StdInLen AS LONG, DecLen AS LONG
                  LOCAL sReply AS STRING, sTemp AS STRING  
                  LOCAL FCGXReq AS FCGX_REQUEST
                        OPEN  "test.log" FOR OUTPUT LOCK WRITE AS #1
                        PRINT #1, "Open " & date$ & "-" & time$
                        if i then print #1, "Init error" : exit function
                        print #1, "OK"
                ' Intialize Databases etc here
                    '- Create the STDIN/STDOUT/STDERR buffers, Connect with the Web Server
                    IF FCGX_InitRequest(FCGXReq, 0, 0) < 0 THEN print #1, "Init Failed" : EXIT FUNCTION ' Error Occured 
                      '- Execution blocked here until an HTTP request arrives
                      IF FCGX_Accept_r(FCGXReq) < 0 THEN  
                       'IF RetVal <> -1 THEN PRINT #hClientDbg, [email protected] ' Error Occured not SIGTERM 
                        EXIT LOOP  
                      END IF 
                      '- The reply must begin with a valid HTTP header
                      sReply = "Content-Type: text/html" + $CRLF+$CRLF 
                      'sReply = sReply + "ReqCount: " + STR$(FCGXReq.ReqCount)+"<BR>" ' Request Count              
                      sReply = sReply + "ReqCount: " + "Not Available" + "<BR>" ' Request Count              
                LOCAL s AS DWORD
                LOCAL p AS ASCIIZ PTR
                    s = FCGX_GetParam("QUERY_STRING", BYVAL FCGXReq.envp)
                    IF p THEN
                        sReply = sReply + "'GET' Data: <BR>"   
                        sReply = sReply + TRIM$(@p) + "<BR>"
                    END IF  
                    sReply = sReply + "<BR>" 
                      '- Echo the Query String
                      'IF LEN([email protected]) THEN  
                        'sReply = sReply + "'GET' Data: <BR>"   
                        'sReply = sReply + [email protected] + "<BR>"
                      'END IF  
                      'sReply = sReply + "<BR>" 
                      '- Echo any POST data
                      'Removed the Post procedure. Very different with FCGI.DLL
                      '- Put the Reply in the STDOUT buffer
                      'IF FCGX_PutStr(STRPTR(sReply), LEN(sReply), byval FCGXReq.pOut) < 0 THEN EXIT LOOP ' Error Occured 
                      IF FCGX_PutS(STRPTR(sReply), byval FCGXReq.pOut) < 0 THEN EXIT LOOP ' Error Occured
                      '- Flush Buffers, Send Request Termination Record, Empty the STDIN/STDOUT/STDERR buffers
                      FCGX_Finish_r FCGXReq
                    close #1
                ' Close Databases etc here
                END FUNCTION