Fixed cWebPlugin_Lua being not thread safe. And I don't know why, but it still crashes in Lua sometimes o_O
WebAdmin chat now supports infinite number of chat messages (only client side). The client requests only NEW chat messages from the server. git-svn-id: http://mc-server.googlecode.com/svn/trunk@776 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
e086b931ea
commit
0da4f7eaa2
@ -1,4 +1,5 @@
|
|||||||
local CHAT_HISTORY = 50
|
local CHAT_HISTORY = 50
|
||||||
|
local LastMessageID = 0
|
||||||
|
|
||||||
local JavaScript = [[
|
local JavaScript = [[
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
@ -24,7 +25,7 @@ local JavaScript = [[
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
function OpenPage( url, callback )
|
function OpenPage( url, postParams, callback )
|
||||||
{
|
{
|
||||||
var xhr = createXHR();
|
var xhr = createXHR();
|
||||||
xhr.onreadystatechange=function()
|
xhr.onreadystatechange=function()
|
||||||
@ -34,25 +35,37 @@ local JavaScript = [[
|
|||||||
callback( xhr )
|
callback( xhr )
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.open("GET", url , true);
|
xhr.open( (postParams!=null)?"POST":"GET", url , true);
|
||||||
xhr.send(null);
|
if( postParams != null )
|
||||||
|
{
|
||||||
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||||
|
}
|
||||||
|
xhr.send(postParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
function LoadPageInto( url, storage )
|
function LoadPageInto( url, postParams, storage )
|
||||||
{
|
{
|
||||||
OpenPage( url, function( xhr )
|
OpenPage( url, postParams, function( xhr )
|
||||||
{
|
{
|
||||||
var ScrollBottom = storage.scrollTop + storage.offsetHeight;
|
var ScrollBottom = storage.scrollTop + storage.offsetHeight;
|
||||||
var bAutoScroll = (ScrollBottom >= storage.scrollHeight); // Detect whether we scrolled to the bottom of the div
|
var bAutoScroll = (ScrollBottom >= storage.scrollHeight); // Detect whether we scrolled to the bottom of the div
|
||||||
|
|
||||||
storage.innerHTML = xhr.responseText;
|
results = xhr.responseText.split("<<divider>>");
|
||||||
|
if( results[2] != LastMessageID ) return; // Check if this message was meant for us
|
||||||
|
|
||||||
|
LastMessageID = results[1];
|
||||||
|
if( results[0] != "" )
|
||||||
|
{
|
||||||
|
storage.innerHTML += results[0];
|
||||||
|
|
||||||
if( bAutoScroll == true )
|
if( bAutoScroll == true )
|
||||||
{
|
{
|
||||||
storage.scrollTop = storage.scrollHeight;
|
storage.scrollTop = storage.scrollHeight;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +74,8 @@ local JavaScript = [[
|
|||||||
var MessageContainer = document.getElementById('ChatMessage');
|
var MessageContainer = document.getElementById('ChatMessage');
|
||||||
if( MessageContainer.value == "" ) return;
|
if( MessageContainer.value == "" ) return;
|
||||||
|
|
||||||
OpenPage( "/~webadmin/Core/Chat/?ChatMessage=" + MessageContainer.value, function( xhr )
|
var postParams = "ChatMessage=" + MessageContainer.value;
|
||||||
|
OpenPage( "/~webadmin/Core/Chat/", postParams, function( xhr )
|
||||||
{
|
{
|
||||||
RefreshChat();
|
RefreshChat();
|
||||||
} );
|
} );
|
||||||
@ -70,19 +84,23 @@ local JavaScript = [[
|
|||||||
|
|
||||||
function RefreshChat()
|
function RefreshChat()
|
||||||
{
|
{
|
||||||
LoadPageInto('/~webadmin/Core/Chat/?JustChat=true', document.getElementById('ChatDiv'));
|
var postParams = "JustChat=true&LastMessageID=" + LastMessageID;
|
||||||
|
LoadPageInto("/~webadmin/Core/Chat/", postParams, document.getElementById('ChatDiv'));
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(RefreshChat, 1000);
|
setInterval(RefreshChat, 1000);
|
||||||
window.onload = RefreshChat;
|
window.onload = RefreshChat;
|
||||||
|
|
||||||
|
var LastMessageID = 0;
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local ChatLogMessages = {}
|
local ChatLogMessages = {}
|
||||||
|
|
||||||
function AddMessage( PlayerName, Message )
|
function AddMessage( PlayerName, Message )
|
||||||
table.insert( ChatLogMessages, { name = PlayerName, message = Message } )
|
LastMessageID = LastMessageID + 1
|
||||||
|
table.insert( ChatLogMessages, { name = PlayerName, message = Message, id = LastMessageID } )
|
||||||
while( #ChatLogMessages > CHAT_HISTORY ) do
|
while( #ChatLogMessages > CHAT_HISTORY ) do
|
||||||
table.remove( ChatLogMessages, 1 )
|
table.remove( ChatLogMessages, 1 )
|
||||||
end
|
end
|
||||||
@ -93,25 +111,29 @@ function OnChat( Player, Message )
|
|||||||
end
|
end
|
||||||
|
|
||||||
function HandleRequest_Chat( Request )
|
function HandleRequest_Chat( Request )
|
||||||
if( Request.Params["JustChat"] ~= nil ) then
|
if( Request.PostParams["JustChat"] ~= nil ) then
|
||||||
|
local LastIdx = 0
|
||||||
|
if( Request.PostParams["LastMessageID"] ~= nil ) then LastIdx = tonumber( Request.PostParams["LastMessageID"] ) end
|
||||||
local Content = ""
|
local Content = ""
|
||||||
for key, value in pairs(ChatLogMessages) do
|
for key, value in pairs(ChatLogMessages) do
|
||||||
|
if( value.id > LastIdx ) then
|
||||||
Content = Content .. "[" .. value.name .. "]: " .. value.message .. "<br>"
|
Content = Content .. "[" .. value.name .. "]: " .. value.message .. "<br>"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
Content = Content .. "<<divider>>" .. LastMessageID .. "<<divider>>" .. LastIdx
|
||||||
return Content
|
return Content
|
||||||
end
|
end
|
||||||
|
|
||||||
if( Request.Params["ChatMessage"] ~= nil ) then
|
if( Request.PostParams["ChatMessage"] ~= nil ) then
|
||||||
LOG("Chat msg: " .. Request.Params["ChatMessage"] )
|
local Message = "[WebAdmin]: " .. Request.PostParams["ChatMessage"]
|
||||||
local Message = "[WebAdmin]: " .. Request.Params["ChatMessage"]
|
|
||||||
cRoot:Get():GetServer():SendMessage( Message )
|
cRoot:Get():GetServer():SendMessage( Message )
|
||||||
AddMessage("WebAdmin", Request.Params["ChatMessage"] )
|
AddMessage("WebAdmin", Request.PostParams["ChatMessage"] )
|
||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local Content = JavaScript
|
local Content = JavaScript
|
||||||
Content = Content .. [[
|
Content = Content .. [[
|
||||||
<div style="font-family: Courier; border: 1px solid #DDD; padding: 10px; width: 97%; height: 200px; overflow: scroll;" id="ChatDiv">Chat messageessss</div>
|
<div style="font-family: Courier; border: 1px solid #DDD; padding: 10px; width: 97%; height: 200px; overflow: scroll;" id="ChatDiv"></div>
|
||||||
<input type="text" id="ChatMessage" onKeyPress="if (event.keyCode == 13) { SendChatMessage(); }"><input type="submit" value="Submit" onClick="SendChatMessage();">
|
<input type="text" id="ChatMessage" onKeyPress="if (event.keyCode == 13) { SendChatMessage(); }"><input type="submit" value="Submit" onClick="SendChatMessage();">
|
||||||
]]
|
]]
|
||||||
return Content
|
return Content
|
||||||
|
@ -49,6 +49,8 @@ public: //tolua_export
|
|||||||
lua_State* GetLuaState() { return m_LuaState; }
|
lua_State* GetLuaState() { return m_LuaState; }
|
||||||
|
|
||||||
cWebPlugin_Lua* CreateWebPlugin(lua_State* a_LuaState); //tolua_export
|
cWebPlugin_Lua* CreateWebPlugin(lua_State* a_LuaState); //tolua_export
|
||||||
|
|
||||||
|
cCriticalSection & GetCriticalSection() { return m_CriticalSection; }
|
||||||
private:
|
private:
|
||||||
bool PushFunction( const char* a_FunctionName, bool a_bLogError = true );
|
bool PushFunction( const char* a_FunctionName, bool a_bLogError = true );
|
||||||
bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
|
bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
|
||||||
|
@ -79,6 +79,7 @@ bool cWebPlugin_Lua::AddTab( const char* a_Title, lua_State * a_LuaState, int a_
|
|||||||
|
|
||||||
std::string cWebPlugin_Lua::HandleRequest( HTTPRequest* a_Request )
|
std::string cWebPlugin_Lua::HandleRequest( HTTPRequest* a_Request )
|
||||||
{
|
{
|
||||||
|
cCSLock( m_Plugin->GetCriticalSection() );
|
||||||
lua_State* LuaState = m_Plugin->GetLuaState();
|
lua_State* LuaState = m_Plugin->GetLuaState();
|
||||||
std::string RetVal = "";
|
std::string RetVal = "";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user