| Blockland Forums > Modification Help |
| How to get torque to connect to a url (GET requests) |
| << < (2/2) |
| Fluff-is-back:
Just to clarify, HTTPObjects do not handle the '&' sign correctly and so cannot work with multiple variables. However TCPObjects can. |
| Port:
You may find this useful: --- Code: ---function pswSocket::onAdd( %this ) { %this.isShuttingDown = false; // used for ::shutDown() %this.connected = false; // used for several things, most notably ::attemptConnect() %this.queueSize = 0; // size of currently cached requests to send %this.queueState = 0; // current state // 0: ready for new request // 1: waiting for response header // 2: waiting for response content // 3: parsing (not used) %this.host = "127.0.0.1"; // IP address of Python WSGI server. %this.port = 80; // Port of Python WSGI server. %this.request = "/serverAPI/%s"; // API path on Python WSGI server. %this.termChar = "\xFF\xFA\xAA\xAF"; // Character sequence for signalling end of response data (for backwards compatibility). %this.attemptConnect(); } function pswSocket::shutDown( %this ) { if ( %this.isShuttingDown ) { return; } %this.isShuttingDown = true; if ( %this.connected ) { %this.disconnect(); } else { %this.schedule( 0, "delete" ); } } function pswSocket::attemptConnect( %this ) { cancel( %this.attemptConnect ); if ( %this.connected ) { return; } %this.connect( %this.host @ ":" @ %this.port ); %this.attemptConnect = %this.schedule( 30000, "attemptConnect" ); } function pswSocket::pushRequest( %this, %request ) { %this.queueElement[ %this.queueSize ] = %request; %this.queueSize++; %this.runQueue(); // If we're already doing something, this function will take care of terminating. } function pswSocket::onConnected( %this ) { %this.connected = true; %this.runQueue(); if ( isEventPending( %this.attemptConnect ) ) { cancel( %this.attemptConnect ); } } function pswSocket::onDisconnect( %this ) { %this.connected = false; %this.queueState = 0; // Reset to ready for new request, we will never get our response if we were waiting for one. if ( !%this.isShuttingDown ) { %this.attemptConnection = %this.schedule( 1000, "attemptConnect" ); } else { %this.schedule( 0, "delete" ); } } function pswSocket::onLine( %this, %line ) { if ( %this.queueState != 1 && %this.queueState != 2 ) // Make sure we want the data. { return; } // Handle headers. if ( %this.queueState == 1 ) { // Add our data. %this.responseHead = %this.responseHead @ %line; // Do we have a double-newline (signalling end of headers)? %pos = strPos( %this.responseHead, "\r\n\r\n" ); if ( %pos >= 0 ) { // Get left-over data. %leftover = getSubStr( %this.responseHead, %pos + 4, strLen( %this.responseHead ) ); // Strip so we only have the header and increase state to data. %this.responseHead = getSubStr( %this.responseHead, 0, %pos ); %this.queueState++; // See if we have a Content-Length header (new server). %pos = strPos( %this.responseHead, "\r\nContent-Length: " ); if ( %pos >= 0 ) { %pos += 18; %end = striPos( %this.responseHead, "\r\n", %pos ); if ( %end >= 0 ) { %this.contentLength = getSubStr( %this.responseHead, %pos, %end - %pos ); } else { %this.contentLength = getSubStr( %this.responseHead, %pos, strLen( %this.responseHead ) ); } } } } else if ( %this.queueState == 2 ) // Handle data (else because we directly increase it above). { // Add our data. %this.responseData = %this.responseData @ %line; // Do we have the Content-Length header (new server)? if ( %this.contentLength >= 0 ) { // If so, check if we exceeded. if ( strLen( %this.responseData ) >= %this.contentLength ) { // Make sure we haven't recieved too much. %this.responseData = getSubStr( %this.responseData, 0, %this.contentLength ); // Send back a callback for response. if ( isFunction( %func = "PSW_onRequestDone" ) ) { call( %func, %this.responseHead, %this.responseData, %this.requestData ); } // Reset our state and get ready for another request. %this.queueState = 0; %this.runQueue = %this.schedule( 32, "runQueue" ); // TODO: make request throttle a setting } } else { // See if we got the termination character sequence (old servers are stupid, but we have to keep backwards compatibility). %pos = strPos( %this.responseData, %this.termChar ); if ( %pos >= 0 ) { // Remove everything until the termination character sequence. %this.responseData = getSubStr( %this.responseData, 0, %pos ); // Send back a callback for response. if ( isFunction( %func = "PSW_onRequestDone" ) ) { call( %func, %this.responseHead, %this.responseData, %this.requestData ); } // Reset our state and get ready for another request. %this.queueState = 0; %this.runQueue = %this.schedule( 32, "runQueue" ); // TODO: make request throttle a setting } } } } function pswSocket::runQueue( %this ) { cancel( %this.runQueue ); if ( %this.queueState != 0 ) // Make sure we're ready for a new request. { return; } if ( !%this.queueSize ) // Make sure we have something to request. { return; } // Remove any left-over data from last request. %this.requestData = ""; %this.responseHead = ""; %this.responseData = ""; %this.contentLength = -1; // Get request that patiently waited the longest (is at front of queue). %this.requestData = %request = %this.queueElement[ 0 ]; // Remove it from the list and push all elements one tick further in the queue. for ( %i = 0 ; %i < %this.queueSize + 1 ; %i++ ) { %this.queueElement[ %i ] = %this.queueElement[ %i + 1 ]; } // We removed an element, decrease the size counter. %this.queueSize--; // Build HTTP request. %method = "GET"; %path = strReplace( %this.request, "%s", %request ); %http = "HTTP/1.1"; %headers = "User-Agent: Torque/1.0 HTTPQueue/1.1"; %request = %method SPC %path SPC %http @ "\r\n" %headers @ "\r\n\r\n"; // Set up stuff for getting the response early to be on the safe side. %this.queueState++; %this.responseHead = ""; %this.responseData = ""; // Send our request. %this.send( %request ); } --- End code --- |
| Navigation |
| Message Index |
| Previous page |