2009/08/25

ネタ的にはZIGOROuさんかhasegawaさんのネタっぽいが...
@if(0)==(0) ECHO OFF
CScript.exe //NoLogo //E:JScript "%~f0" %*
GOTO :EOF
@end

function wsock_ConnectionRequest(reqId) {    
    if (socket.State != 0/* closed */) socket.Close();
    socket.Accept(reqId);
}

function wsock_DataArrival(bytesTotal) {    
    var data = script.Run('GetData', socket, bytesTotal);
    socket.SendData([
        "HTTP/1.1 200 OK",
        "Connection: closed",
        "Content-Type: text/html;",
        "",
        "Hello World! " + new Date(),
        ""
    ].join("\n"));
    // 相手が閉じてくれないので閉じたいけど待たないとレスポンスが無くなる
    WScript.Sleep(1000);
    socket.Close();
    socket.Listen();
}

var socket = WScript.CreateObject('MSWinsock.Winsock', 'wsock_');
var script = WScript.CreateObject('ScriptControl');
script.language = 'VBScript';
script.AddObject('WScript', WScript);
script.AddCode([
    'Function GetData(socket, bytesTotal):',
    '  Dim data:',
    '  socket.GetData data, vbString, bytesTotal:',
    '  GetData = data:',
    'End Function'
].join(''));

socket.Bind(8080);
socket.Listen();
while (socket.State != 9/* error */{ 
    WScript.Sleep(100);
} 

// vim:set ft=javascript:
GetDataがByRefなので、ScriptControlを使ってます。
Posted at 11:51 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.