Skip to content

Commit 9b5a457

Browse files
committed
ver 1.0.3
Sync changes from sm-ext-yyjson Updated some HTTP functions
1 parent 624681c commit 9b5a457

10 files changed

+1401
-519
lines changed

README.md

+3-137
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ambuild
2828
* [websocket](https://github.com/ProjectSky/sm-ext-websocket/blob/main/scripting/include/websocket/ws.inc)
2929

3030
# Binary files
31-
* https://github.com/ProjectSky/sm-ext-websocket/actions
31+
* [GitHub Releases](https://github.com/ProjectSky/sm-ext-websocket/releases)
3232

3333
## TODO
3434
- [x] WebSocket server support
@@ -39,139 +39,5 @@ ambuild
3939
* HTTP functionality is not yet complete. Currently, only basic features are available
4040
* Server will not process data during the hibernation. You can set sv_hibernate_when_empty to 0 to disable hibernation
4141

42-
## Client Example
43-
``` c++
44-
#include <sourcemod>
45-
#include <websocket>
46-
47-
WebSocket
48-
g_hWS;
49-
50-
public void OnPluginStart()
51-
{
52-
RegServerCmd("ws_start", ws_start);
53-
RegServerCmd("ws_close", ws_close);
54-
RegServerCmd("ws_state", ws_state);
55-
}
56-
57-
Action ws_start(int args)
58-
{
59-
g_hWS = new WebSocket("ws://127.0.0.1:9999", WebSocket_JSON);
60-
g_hWS.SetMessageCallback(onMessage);
61-
g_hWS.SetOpenCallback(onOpen);
62-
g_hWS.SetCloseCallback(onClose);
63-
g_hWS.SetErrorCallback(onError)
64-
g_hWS.Connect();
65-
}
66-
67-
Action ws_close(int args)
68-
{
69-
g_hWS.Disconnect();
70-
return Plugin_Handled;
71-
}
72-
73-
Action ws_state(int args)
74-
{
75-
PrintToServer("ReadyState: %d", g_hWS.ReadyState);
76-
return Plugin_Handled;
77-
}
78-
79-
void onOpen(WebSocket ws)
80-
{
81-
PrintToServer("onOpen: %x", ws);
82-
}
83-
84-
void onClose(WebSocket ws, int code, const char[] reason)
85-
{
86-
PrintToServer("onClose: %d, %s", code, reason);
87-
}
88-
89-
void onMessage(WebSocket ws, const YYJSON message, int wireSize)
90-
{
91-
char[] buffer = new char[wireSize];
92-
message.ToString(buffer, wireSize);
93-
PrintToServer("msg: %s, size: %d", buffer, wireSize);
94-
}
95-
96-
void onError(WebSocket ws, const char[] errMsg)
97-
{
98-
PrintToServer("onError: %s", errMsg);
99-
}
100-
```
101-
102-
## Server Example
103-
``` c++
104-
#include <sourcemod>
105-
#include <websocket>
106-
107-
WebSocketServer
108-
g_hWsServer;
109-
110-
public void OnPluginStart()
111-
{
112-
RegServerCmd("ws_server", ws_server);
113-
RegServerCmd("ws_server_stop", ws_server_stop);
114-
RegServerCmd("ws_server_broadcast", ws_server_broadcast);
115-
RegServerCmd("ws_server_clients", ws_server_clients);
116-
RegServerCmd("ws_server_sendtoclient", ws_server_sendtoclient);
117-
}
118-
119-
Action ws_server(int args)
120-
{
121-
g_hWsServer = new WebSocketServer("0.0.0.0", 9999);
122-
g_hWsServer.SetMessageCallback(onSrvMessage);
123-
g_hWsServer.SetOpenCallback(onSrvOpen);
124-
g_hWsServer.SetCloseCallback(onSrvClose);
125-
g_hWsServer.SetErrorCallback(onSrvError);
126-
g_hWsServer.Start();
127-
return Plugin_Handled;
128-
}
129-
130-
Action ws_server_broadcast(int args)
131-
{
132-
g_hWsServer.BroadcastMessage("Broadcast Message");
133-
return Plugin_Handled;
134-
}
135-
136-
Action ws_server_sendtoclient(int args)
137-
{
138-
char clientId[4], message[256];
139-
GetCmdArg(1, clientId, sizeof(clientId));
140-
GetCmdArg(2, message, sizeof(message));
141-
142-
g_hWsServer.SendMessageToClient(clientId, message);
143-
return Plugin_Handled;
144-
}
145-
146-
Action ws_server_clients(int args)
147-
{
148-
PrintToServer("Connected clients: %d", g_hWsServer.ClientsCount)
149-
return Plugin_Handled;
150-
}
151-
152-
Action ws_server_stop(int args)
153-
{
154-
g_hWsServer.Stop();
155-
return Plugin_Handled;
156-
}
157-
158-
void onSrvMessage(WebSocketServer ws, WebSocket client, const char[] message, int wireSize, const char[] RemoteAddr, const char[] RemoteId)
159-
{
160-
PrintToServer("message: %s, wireSize: %d, RemoteAddr: %s, RemoteId: %s", message, wireSize, RemoteAddr, RemoteId);
161-
}
162-
163-
void onSrvError(WebSocketServer ws, const char[] errMsg, const char[] RemoteAddr, const char[] RemoteId)
164-
{
165-
PrintToServer("onError: %s, RemoteAddr: %s, RemoteId: %s", errMsg, RemoteAddr, RemoteId);
166-
}
167-
168-
void onSrvOpen(WebSocketServer ws, const char[] RemoteAddr, const char[] RemoteId)
169-
{
170-
PrintToServer("onOpen: %x, RemoteAddr: %s, RemoteId: %s", ws, RemoteAddr, RemoteId);
171-
}
172-
173-
void onSrvClose(WebSocketServer ws, int code, const char[] reason, const char[] RemoteAddr, const char[] RemoteId)
174-
{
175-
PrintToServer("onClose: %d, reason: %s, RemoteAddr: %s, RemoteId: %s", code, reason, RemoteAddr, RemoteId);
176-
}
177-
```
42+
## Example
43+
* [Example Script](https://github.com/ProjectSky/sm-ext-websocket/tree/main/scripting)

pushbuild.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
0x0
22
0x1
3-
0x2
3+
0x2
4+
0x3

scripting/include/websocket/http.inc

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// HTTP FUNCTIONS ARE NOT COMPLETED, THEY ARE STILL BEING TESTED
2+
13
// Define a typeset for HTTP response callbacks
24
typeset ResponseCallback
35
{
@@ -7,8 +9,9 @@ typeset ResponseCallback
79
* @param http HTTP request object
810
* @param body Response body
911
* @param statusCode HTTP status code
12+
* @param bodySize Size of the response body
1013
*/
11-
function void (HttpRequest http, const char[] body, int statusCode);
14+
function void (HttpRequest http, const char[] body, int statusCode, int bodySize);
1215
}
1316

1417
// Define a methodmap for HttpRequest, which extends Handle
@@ -29,8 +32,41 @@ methodmap HttpRequest < Handle
2932
*/
3033
public native void SetResponseCallback(ResponseCallback fResponse);
3134

32-
/**
33-
* Perform the HTTP request
34-
*/
35+
/**
36+
* Perform the HTTP request
37+
*/
3538
public native void Perform();
39+
40+
/**
41+
* Set the body of the HTTP request
42+
*
43+
* @param body Body of the request
44+
*/
45+
public native void SetBody(const char[] body);
46+
47+
/**
48+
* Add a header to the HTTP request
49+
*
50+
* @param key Header key
51+
* @param value Header value
52+
*/
53+
public native void AddHeader(const char[] key, const char[] value);
54+
55+
/**
56+
* Get a header from the HTTP request
57+
*
58+
* @param key Header key
59+
* @param value Buffer to store the header value
60+
* @param maxLength Maximum length of the value buffer
61+
* @return True if the header was found, false otherwise
62+
*/
63+
public native bool GetHeader(const char[] key, char[] value, int maxLength);
64+
65+
/**
66+
* Check if a header exists in the HTTP request
67+
*
68+
* @param key Header key
69+
* @return True if the header exists, false otherwise
70+
*/
71+
public native bool HasHeader(const char[] key);
3672
}

scripting/include/websocket/ws.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ methodmap WebSocket < Handle
172172
/**
173173
* Set the handshake timeout for the WebSocket connection
174174
*
175-
* @param timeout timeout in seconds
175+
* @param timeout in seconds
176176
*/
177177
property int HandshakeTimeout {
178178
public native set(int timeout);

0 commit comments

Comments
 (0)