Skip to content

Commit

Permalink
Refactor onMessage event
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellslicer committed Aug 31, 2017
1 parent a098a72 commit ef58cc6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("0.1.0")]
[assembly: AssemblyVersion("0.2.0")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ Convars available:
### Add a listener to receive messages

```lua
-- Create a listener with a string parameter
AddEventHandler("my:listener", function(message)
-- Add a new listener with a string parameter
AddEventHandler("WebSocketServer:onMessage", function(message)
print("Received message: " .. message)
end)

-- Add your listener to WebSockerServer
TriggerEvent("WebSocketServer:addListener", "my:listener");
```

### Send a message to connected WebSocket clients
Expand Down
21 changes: 1 addition & 20 deletions WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,13 @@ private enum LogLevels { Debug, Info, Warn, Error };
private LogLevels logLevel;

private string authorization;
private List<string> events;
private List<WebSocket> webSockets;

public WebSocketServer()
{
logLevel = Function.Call<string>(Hash.GET_CONVAR, "websocket_debug", "false") == "true" ? LogLevels.Debug : LogLevels.Info;
authorization = Function.Call<string>(Hash.GET_CONVAR, "websocket_authorization", "");

events = new List<string>();
EventHandlers["WebSocketServer:addListener"] += new Action<dynamic>((dynamic eventName) =>
{
lock (events)
{
if (!events.Contains((string) eventName))
{
events.Add((string) eventName);
}
}
});

webSockets = new List<WebSocket>();
EventHandlers["WebSocketServer:broadcast"] += new Action<dynamic>((dynamic message) =>
{
Expand Down Expand Up @@ -94,13 +81,7 @@ public WebSocketServer()
{
Log("Received message: " + msg, LogLevels.Debug);

lock (events)
{
foreach (var eventName in events)
{
TriggerEvent(eventName, msg);
}
}
TriggerEvent("WebSocketServer:onMessage", msg);
};

try
Expand Down

0 comments on commit ef58cc6

Please sign in to comment.