Ulysses

General => Off-Topic => Topic started by: azarus on April 09, 2015, 09:18:13 AM

Title: Global Chat Or Websocket extension?
Post by: azarus on April 09, 2015, 09:18:13 AM
Hey Folks,


I am looking for a Global Chat addon or a websocket extension.
My goal is to connect and replicate the chat for multiple servers + a web chat.

So if you know a good starting point i would appreciate it.

If there is no such addon that provides good interface for this, do you know a websocket extension?
If there is no weboscket extension available yet, which socket extension should i use or you recommend? I need async support!

Thanks! :)

- Azarus
Title: Re: Global Chat Or Websocket extension?
Post by: Bytewave on April 09, 2015, 02:23:40 PM
I recall seeing something like this a while back, but I don't think it was here.
Regardless, I do know of a decent networking socket module, gm_bromsock (http://facepunch.com/showthread.php?t=1393640).
Title: Re: Global Chat Or Websocket extension?
Post by: JamminR on April 09, 2015, 04:03:39 PM
http://forums.ulyssesmod.net/index.php/topic,3415.0.html (http://forums.ulyssesmod.net/index.php/topic,3415.0.html)
Maybe MrPres will get bored and whip up more fun stuff.
Though server monitoring was on his mind at that time, wouldnt doubt it's chat capability.
(But, I didn't go read into it just now, perhaps it was already mentioned)
It's not complete. Just something he was tinkering with years ago.
Title: Re: Global Chat Or Websocket extension?
Post by: MrPresident on April 09, 2015, 05:49:25 PM
I have been meaning to tinker around with luasock again.
What's with you guys and linking to old stuff I've done lately. The nostalgia is killing me...
Title: Re: Global Chat Or Websocket extension?
Post by: JamminR on April 09, 2015, 08:07:28 PM
People are finally asking for stuff related to projects you need to finish.
Could be worse. You could be like famous artists.
They're work only makes big money after death.
:)
Title: Re: Global Chat Or Websocket extension?
Post by: MrPresident on April 09, 2015, 09:38:06 PM
The problem is, all of these projects are so old I don't have them anymore. I've gone through a couple computers and a few major life events since some of these, and I wasn't the best at saving copies of old projects unfortunately.
Title: Re: Global Chat Or Websocket extension?
Post by: azarus on April 10, 2015, 07:52:37 AM
Awesome, and thanks for all the replies! :)
This gm_bromsock socket extension looks useful. I am hoping to have something to show off by tonight  ::)
Title: Re: Global Chat Or Websocket extension?
Post by: Buzzkill on April 10, 2015, 11:23:24 AM
(sigh)... more stuff to distract me from reality....
Title: Re: Global Chat Or Websocket extension?
Post by: azarus on April 12, 2015, 04:11:03 AM
That gm_brosock is horrible :(
It blocked the game thread a few times. (Needed to restart the server)
And the recvfrom function is not returning when its set to non-blocking and so it blocks the send function.

So i decided to write my own which fits the task.
https://github.com/azarus/GMSocks
It has only UDP support for now, but it can be and will be extended.
Title: Re: Global Chat Or Websocket extension?
Post by: MrPresident on May 02, 2015, 04:50:39 PM
Azarus,
I have a question about your UDP Socket module.
Would you run the UDPReceive function every tick? You should make it a callback or hook rather than something you have to manually check to see if they've received data.
Title: Re: Global Chat Or Websocket extension?
Post by: azarus on May 09, 2015, 12:59:15 AM
You totally can check for incoming packets at every tick, it shouldn't slow down the app. But it's just pointless. About the event thing, you would do the same thing in c++  with a recvfrom(); no matter what.
But i believe you can do the rest event thing in lua and it is easier :)

Anyways here is an example use of the udp module. Sends all the incoming chat messages to a remote server, and listens to incoming messages.
Code: [Select]
require("socks");

local Cfg = {};
Cfg.IP = "127.0.0.1";
Cfg.Port = 1234;
Cfg.Password = "******";

util.AddNetworkString('GlobalChatMessage');
util.AddNetworkString('GlobalChatUser');

local function GetServerIP()
        local hostip = GetConVarString( "hostip" )
        hostip = tonumber( hostip )
       
        local ip = {}
        ip[ 1 ] = bit.rshift( bit.band( hostip, 0xFF000000 ), 24 )
        ip[ 2 ] = bit.rshift( bit.band( hostip, 0x00FF0000 ), 16 )
        ip[ 3 ] = bit.rshift( bit.band( hostip, 0x0000FF00 ), 8 )
        ip[ 4 ] = bit.band( hostip, 0x000000FF )
       
        return table.concat( ip, "." )
end

hook.Add( "PlayerSay", "GlobalChatPlayerSay",  function( ply, text, public )

local Query = {};
Query.Password = Cfg.Password;
Query.Type = "chat";
Query.Body = {};
Query.Body.Group = ply:GetUserGroup();
Query.Body.PlayerName = ply:Nick();
Query.Body.SteamID = ply:SteamID();
Query.Body.Message = text;

local JsonString = util.TableToJSON( Query );


UDPSend(Cfg.Port, Cfg.IP, JsonString, string.len(JsonString) );


end);




timer.Create("GlobalChatConnection", 0.1, 0, function()
local data = UDPReceive(Cfg.Port, Cfg.IP);

if data == nil then
return;

end

local JSONIn = util.JSONToTable(data);
if !JSONIn then
return;
end

if JSONIn.Pong or JSONIn.Ping then
return
end

if JSONIn.Type == "chat" then
net.Start("GlobalChatMessage");
net.WriteString(JSONIn.Body.PlayerName);

if JSONIn.Body.Group != nil then -- We have different versions
net.WriteBool( true );
net.WriteString(JSONIn.Body.Group);
else
net.WriteBool( false );
end
net.WriteString(JSONIn.Body.Message);
net.Broadcast();
end

if JSONIn.Type == "user" then
net.Start("GlobalChatUser");
net.WriteString(JSONIn.Body.IP); -- Write Server IP
net.WriteInt(JSONIn.Body.PlayerCount, 16); -- Write Player Count
for key, ply in pairs(JSONIn.Body.Players) do
net.WriteString(JSONIn.Body.Players[key].Nick); -- Write Player[X] Nickname
net.WriteString(JSONIn.Body.Players[key].SteamID); -- Write Player[X] Nickname
net.WriteString(JSONIn.Body.Players[key].Group); -- Write Player[X] Group
end
net.Broadcast();
end

end);

-- This is an important message. It keeps the server in the listeners list for the master server
timer.Create("GlobalChatPing", 10, 0, function()
if #player.GetAll() > 0 then
local Query = {};
Query.Password = Cfg.Password;
Query.Ping = true;

-- Send the packet
local packet = util.TableToJSON( Query );

UDPSend(Cfg.Port, Cfg.IP, packet, string.len(packet) );

end
end);

-- udp:Connect(Cfg.IP, Cfg.Port);


function GlobalChat_UpdatePlayerState( unused )
SendChatState();
end

function SendChatState()
local Query = {};
Query.Password = Cfg.Password;
Query.Type = "user";
Query.Body = {};
Query.Body.IP = GetServerIP() .. ":"..GetConVarString("hostport");
Query.Body.Players = {};
Query.Body.PlayerCount = #player.GetAll();

for key, ply in pairs(player.GetAll()) do
Query.Body.Players[key] = {};
Query.Body.Players[key].Nick = ply:Nick();
Query.Body.Players[key].SteamID = ply:SteamID();
Query.Body.Players[key].Group = ply:GetUserGroup();
end

local JsonString = util.TableToJSON( Query );
UDPSend(Cfg.Port, Cfg.IP, JsonString, string.len(JsonString) );

end

hook.Add( "PlayerInitialSpawn", "GlobalChat_PlayerInitialSpawn", GlobalChat_UpdatePlayerState );
hook.Add( "PlayerDisconnected", "GlobalChat_PlayerDisconnected", GlobalChat_UpdatePlayerState );