试图实施CFTHREAD ...失败:(

时间:2013-07-07 09:13:17

标签: asynchronous coldfusion xmlhttprequest cfhttp cfthread

我之前发布了cfhttp in cfloop limit? use cfthread关于发出多个CFHTTP请求的帖子,我在JS函数中实现了XMLHttpRequest的解决方案,以便从另一个文件调用我的CFHTTP请求。

我修改了它,以便它在一个intertval计时器上运行,但我似乎有一个问题,我只允许主机每秒发出3个请求,但我可能会将间隔时间设置为每个7秒我仍然最终超时,因为CFHTTP请求似乎在挂起并且经常同时发出请求(这意味着它正在通过阈值)。

我已经阅读了Ben Nadel关于使用CFTHREAD的帖子,但我完全没有乐意实现它们。

目前我的基本流程是: -

  • Index.cfm包含一个JS函数,它使用调用playerSearch.cfm的XMLHttpRequest
  • playerSearch.cfm发出CFHTTP请求。
  • 然后
  • playerSearch.cfm循环响应并显示循环中的当前项目以及提交另一个CFHTTP请求,以便在它满足我的任何IF条件时对循环中的当前项目进行出价。
  • Index.cfm然后每隔X秒继续发出请求。

有没有办法可以设置CFHTTP请求本身每隔X秒发出一次请求(可能使用CFTHREAD中的sleep函数?)以及如何在CFTHREAD中显示任何内容,因为我似乎不是能够吗?

此外,如果我能够让CFHTTP请求本身每隔X秒发出请求,那么我会更好地直接调用它并且它会不断循环吗?

非常感谢任何反馈!

编辑:

Index.cfm

<script>

var searchTimer = null,
    searchInterval = 1000,
    startPosition = 0;

function startSearch() {
    if (searchTimer !== null) return;
    searchTimer = setInterval(function () {
        startPosition = startPosition + 16;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "playerSearch.cfm?startPosition="+startPosition, true);
        xhr.onload = function (e) {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    document.getElementById("counter").innerHTML = xhr.response;
                } else {
                    // Error handling
                }
            }
        };
        xhr.onerror = function (e) {
            // Error handling
        };
        xhr.send(null);
    }, searchInterval);
}

function stopSearch() {
    clearInterval(searchTimer);
    searchTimer = null
}

</script>

playerSearch.cfm

<cfset Variables.startPosition = URL.startPosition />

<cfhttp url="https://utas.fut.ea.com/ut/game/fifa13/auctionhouse?type=player" method="post" result="getPlayer">
    <cfhttpparam type="header" name="Accept" value="application/json" />
    <cfhttpparam type="header" name="Accept-Language" value="en-GB" />
    <cfhttpparam type="header" name="Connection" value="keep-alive" />
    <cfhttpparam type="header" name="Content-Length" value="1" />
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="header" name="COOKIE" value="#Arguments.cookieString#">
    <cfhttpparam type="header" name="Host" value="utas.fut.ea.com" />
    <cfhttpparam type="header" name="Referer" value="http://cdn.easf.www.easports.com/soccer/static/flash/futFifaUltimateTeamPlugin/FifaUltimateTeam.swf" />
    <cfhttpparam type="header" name="User-Agent" value="Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36" />
    <cfhttpparam type="header" name="X-HTTP-Method-Override" value="GET" />
    <cfhttpparam type="header" name="X-UT-Embed-Error" value="true" />
    <cfhttpparam type="header" name="x-flash-version" value="11,7,700,224" />
    <cfhttpparam type="header" name="X-UT-SID" value="#Arguments.sessionID#" />
</cfhttp>

<cfif getPlayer.StatusCode EQ "200 OK">
    <!--- DESERIALIZE RETURNED JSON SO CAN LOOP ROUND EACH RESULT --->
    <cfset Variables.searchResults = DeserializeJSON(getPlayer.FileContent) />

    <!--- IF SEARCH RESULTS RETURNED --->
        <cfset Variables.numResults = ArrayLen(Variables.searchResults.auctionInfo) />
        <cfset Variables.bidsMade = 0 />

        <table width="900" cellpadding="0" cellspacing="0">
            <tr>
                <th width="100" align="left" class="heading">Player Name</th>
                <th width="70" align="left" class="heading">Rating</th>
                <th width="50" align="left" class="heading">Formation</th>
                <th width="80" align="left" class="heading">Position</th>
                <th width="80" align="left" class="heading">Bid Status</th>
                <th width="100" align="left" class="heading">Starting Price</th>
                <th width="80" align="left" class="heading">BIN Price</th>
                <th width="80" align="left" class="heading">Current Bid</th>
                <th width="80" align="left" class="heading">My Bid</th>
                <th width="120" align="left" class="heading">Ends</th>
            </tr>
            <cfloop from="1" to="#Variables.numResults#" index="i"> 

                <cfscript>

                    // DEFAULT BID AMOUNT
                    Variables.bidAmount = 0;

                    // SET BID AMOUNT
                    // IF ITEM HAS BUY NOW PRICE AND IT IS LESS THAN QUICK SELL VALUE THEN BID THAT AMOUNT
                    if (Variables.searchResults.auctionInfo[i].buyNowPrice NEQ 0 AND Variables.searchResults.auctionInfo[i].buyNowPrice LT Variables.searchResults.auctionInfo[i].itemData.discardValue) {
                        Variables.bidAmount = Variables.searchResults.auctionInfo[i].buyNowPrice;
                    }
                    // ELSE IF QUICK SELL VALUE IS 228,231,235,238,242,245 OR 249 BID 200
                    else if (ListFind("228,231,235,238,242,245,249",Variables.searchResults.auctionInfo[i].itemData.discardValue) AND Variables.searchResults.auctionInfo[i].startingBid LTE 200 AND Variables.searchResults.auctionInfo[i].currentBid LT 200) {
                        Variables.bidAmount = 200;
                    }
                    // ELSE IF QUICK SELL VALUE IS 252,256,259 OR 300 BID 250
                    else if (ListFind("252,256,259,300",Variables.searchResults.auctionInfo[i].itemData.discardValue) AND Variables.searchResults.auctionInfo[i].startingBid LTE 250 AND Variables.searchResults.auctionInfo[i].currentBid LT 250) {
                        Variables.bidAmount = 250;
                    }

                    // GET MY CURRENT COIN TOTAL
                    Variables.getCoinTotal = Application.cfcs.Club.getCreditsTotal(SESSION.phishingKey,SESSION.sessionKey);
                    Variables.curCoinsData = DeserializeJSON(Variables.getCoinTotal.FileContent);
                    Variables.curCoins = Variables.curCoinsData.credits;

                    // IF I CURRENTLY HAVE ENOUGH COINS IN ACCOUNT PLACE BID
                    if (StructKeyExists(Variables,"curCoins") AND Variables.bidAmount NEQ 0 AND Variables.bidAmount LT Variables.curCoins) {

                        <cfset Variables.bidData = '{ "bid": ' & Variables.bidAmount & ' }'>
                        <cfset Variables.bidDataLength = Len(Variables.bidData) />

                        <cfhttp url="https://utas.fut.ea.com/ut/game/fifa13/trade/" & Variables.searchResults.auctionInfo[i].tradeID & "/bid" method="POST" result="makeBid">
                            <cfhttpparam type="header" name="Accept" value="application/json" />
                            <cfhttpparam type="header" name="Connection" value="keep-alive" />
                            <cfhttpparam type="header" name="Content-Type" value="application/json" />
                            <cfhttpparam type="header" name="Content-Length" value="#Variables.bidDataLength#" />
                            <cfhttpparam type="header" name="Host" value="utas.fut.ea.com" />
                            <cfhttpparam type="header" name="Referer" value="http://cdn.easf.www.easports.com/soccer/static/flash/futFifaUltimateTeamPlugin/FifaUltimateTeam.swf" />
                            <cfhttpparam type="header" name="X-HTTP-Method-Override" value="PUT" />
                            <cfhttpparam type="header" name="X-UT-SID" value="#SESSION.sessionKey#" />
                            <cfhttpparam type="header" name="COOKIE" value="#SESSION.phishingKey#">     
                            <cfhttpparam type="header" name="User-Agent" value="Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36" />
                            <cfhttpparam type="body" value="#Variables.bidData#" /> 
                        </cfhttp>

                        Variables.bidsMade = Variables.bidsMade + 1;

                    } else {

                        Variables.bidAmount = 0;

                    }
                </cfscript>
                <cfoutput>
                    <tr>
                        <td align="left">#Variables.searchResults.auctionInfo[i].itemData.assetID#</td>
                        <td align="left">
                            #Variables.searchResults.auctionInfo[i].itemData.rating# 
                            <cfif Variables.searchResults.auctionInfo[i].itemData.rareFlag EQ 0>
                                &nbsp;
                            <cfelseif Variables.searchResults.auctionInfo[i].itemData.rareFlag EQ 1>
                                (Rare)
                            <cfelse>
                                (Something else)
                            </cfif>
                        </td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].itemData.formation#</td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].itemData.preferredPosition#</td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].bidState#</td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].startingBid#</td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].buyNowPrice#</td>
                        <td align="left">#Variables.searchResults.auctionInfo[i].currentBid#</td>
                        <td align="left">#Variables.bidAmount#</td>
                        <td align="left">
                            <cfif Variables.searchResults.auctionInfo[i].tradeState EQ "active">
                                <cfset timeLeft = Variables.searchResults.auctionInfo[i].expires />
                                <cfset auctionEnds = DateAdd("s",timeLeft,Now()) />
                                #DateFormat(Variables.auctionEnds,"dd/mm/yyyy")# #TimeFormat(Variables.auctionEnds,"HH:mm:ss")#
                            <cfelse>
                                Ended
                            </cfif>
                        </td>
                    </tr>
                </cfoutput>
            </cfloop>
        </table>
        <br /><br />Bids Made: <cfoutput>#Variables.bidsMade#</cfoutput>
<cfelse><br />
    The search returned an error.
    <cfdump var="#getPlayer#">
</cfif>

0 个答案:

没有答案