Author Topic: Global Chat Or Websocket extension?  (Read 4442 times)

0 Members and 1 Guest are viewing this topic.

Offline azarus

  • Newbie
  • *
  • Posts: 10
  • Karma: 9
  • NC Gaming
    • NC Gaming
Global Chat Or Websocket extension?
« 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
« Last Edit: April 10, 2015, 07:47:40 AM by azarus »

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Global Chat Or Websocket extension?
« Reply #1 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.
bw81@ulysses-forums ~ % whoami
Homepage

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Global Chat Or Websocket extension?
« Reply #2 on: April 09, 2015, 04:03:39 PM »
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.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Global Chat Or Websocket extension?
« Reply #3 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...

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Global Chat Or Websocket extension?
« Reply #4 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.
:)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Global Chat Or Websocket extension?
« Reply #5 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.

Offline azarus

  • Newbie
  • *
  • Posts: 10
  • Karma: 9
  • NC Gaming
    • NC Gaming
Re: Global Chat Or Websocket extension?
« Reply #6 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  ::)

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Global Chat Or Websocket extension?
« Reply #7 on: April 10, 2015, 11:23:24 AM »
(sigh)... more stuff to distract me from reality....

Offline azarus

  • Newbie
  • *
  • Posts: 10
  • Karma: 9
  • NC Gaming
    • NC Gaming
Re: Global Chat Or Websocket extension?
« Reply #8 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.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Global Chat Or Websocket extension?
« Reply #9 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.

Offline azarus

  • Newbie
  • *
  • Posts: 10
  • Karma: 9
  • NC Gaming
    • NC Gaming
Re: Global Chat Or Websocket extension?
« Reply #10 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 );
« Last Edit: November 05, 2018, 06:20:13 PM by azarus »