Create a gist now

Instantly share code, notes, and snippets.

@masterpoppy /Basic Comet.lsl Secret
Last active Dec 21, 2016

What would you like to do?
//***********************
//Basic Comet
//2016-12-10 MasterPoppy
//***********************
string html = "<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<style type='text/css'>
body {
font-family: helvetica, arial, 'hiragino kaku gothic pro', meiryo, 'ms pgothic', sans-serif;
zoom: 120%;
}
h1,p{
padding: 8px;
margin: 0;
}
/*1行おきに背景色を変えて見やすくする*/
p:nth-child(even) {
background-color: #EEEEEE;
}
</style>
<script>
//ページ読み込みでリクエスト開始
$(document).ready(function() {
request();
}, false);
//リクエスト処理
function request() {
$.ajax({
'type': 'POST',
'url': location.href
}).done(function(data) {
// 新しく取得した発言を書き込み
$('body').append('<p>' + data + '</p>');
// 下までスクロール
scrollbottom();
// 再帰呼び出し
request();
}).fail(function(data) {
if (data.status == 504) {
// 504 : Gateway Timeout, タイムアウトしたら再リクエスト
request();
}
});
}
// ページの最下部までスクロールするユーザ関数
function scrollbottom() {
$('body').scrollTop($('body').scrollTop() + $('body').height());
}
</script>
</head>
<body>
<h1>Basic Comet</h1>
</body>
</html>
";
key query_id;
integer status;
key request;
string URI;
integer handle;
integer render_face = 4; //-X
check_memory()
{
float freememory = llGetFreeMemory()/1024.0;
llOwnerSay("freememory = " + (string)freememory+" / 64 kbyte");
}
string ConvertWallclockToTime()
{
integer now = (integer)llGetWallclock();
integer seconds = now % 60;
integer minutes = (now / 60) % 60;
integer hours = now / 3600;
return llGetSubString("0" + (string)hours, -2, -1) + ":"
+ llGetSubString("0" + (string)minutes, -2, -1) + ":"
+ llGetSubString("0" + (string)seconds, -2, -1);
}
default
{
state_entry()
{
llReleaseURL(URI);
request = llRequestURL();
//check_memory();
}
changed(integer change)
{
if(change & CHANGED_REGION || change & CHANGED_REGION_START){
llReleaseURL(URI);
request = llRequestURL();
}
}
attach(key id)
{
if(id){
llReleaseURL(URI);
request = llRequestURL();
}
}
http_request(key id, string method, string body)
{
key owner = llGetOwner();
if (request == id){
request = "";
if (method == URL_REQUEST_GRANTED){
URI = body;
llClearPrimMedia(render_face);
status = llSetLinkMedia(LINK_THIS, render_face, [
PRIM_MEDIA_HOME_URL, URI,
PRIM_MEDIA_CURRENT_URL, URI,
PRIM_MEDIA_AUTO_ZOOM, FALSE,
PRIM_MEDIA_AUTO_PLAY, TRUE,
PRIM_MEDIA_AUTO_SCALE, FALSE,
PRIM_MEDIA_WIDTH_PIXELS, 1024.0,
PRIM_MEDIA_HEIGHT_PIXELS, 1024.0,
PRIM_MEDIA_PERMS_INTERACT, PRIM_MEDIA_PERM_NONE,
PRIM_MEDIA_PERMS_CONTROL, PRIM_MEDIA_PERM_NONE
]);
}
}
else{
if(method == "GET"){
llSetContentType(id, CONTENT_TYPE_HTML);
llHTTPResponse(id, 200, html);
//ページを表示したらListenを開く
llListenRemove(handle);
handle = llListen(0, "", NULL_KEY, "");
}
else if(method == "POST"){
//グローバル変数にhttp requestのIDを保持する(*)
query_id = id;
}
}
}
listen(integer channel, string name, key id, string message)
{
if(query_id != NULL_KEY){
name = llGetDisplayName(id);
if(name != ""){
//保持しておいたHTTP requestのID向けにResponseする(*)
llSetContentType(query_id, CONTENT_TYPE_TEXT);
llHTTPResponse(query_id, 200, ConvertWallclockToTime() + " [" + name + "] " + message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment