Update: As I suspected, I am an idiot. The issue causing the Invalid Handle error was that my code was in OnPluginStart as opposed to OnAllPluginsLoaded. I plan to get around the lack of ability to select a path endpoint (site.com:port/endpoint) by just using an additional port. However I am now experiencing "Unable to listen on ip: port". Investigating whether this is an issue with my webserver though (most likely).
Original post:
Can someone tell me if I have this right:
I can only create listening servers using
Code:
Websocket_Open(host, port, callbacks...)
where host is the game server's IP.
I assume this is the case because both the author's example programs listen on the same server.
This is actually fine while I'm in dev but in production I'll want to have my game servers on a different server than my webserver. However I still must be doing something wrong, because when I try to use Websocket_Open I get this exception: Invalid Handle 0 (error: 4)
Code:
#include <sourcemod>
#include <tf2_stocks>
#include <entity_prop_stocks>
#include <sdkhooks>
#include <morecolors>
#include <websocket>
WebsocketHandle wsHandle = INVALID_WEBSOCKET_HANDLE;
//----------------------------------------excerpt from OnPluginStart:
{
char txtfile[256];
BuildPath(Path_SM, txtfile, sizeof(txtfile), "configs/mything.cfg");
KeyValues kv = new KeyValues("mything");
kv.ImportFromFile(txtfile);
kv.Rewind();
char wsHost[64], wsPortStr[12];
kv.GetString("host", wsHost, 64); //this properly reads as my ip string (x.x.x.x)
kv.GetString("port", wsPortStr, 12);
int wsPort = StringToInt(wsPortStr);
delete kv;
PrintToServer("*+*+*+*+*+*+*+*+*+*+*+*+*+ connecting to server @ %s@%i", wsHost, wsPort); //all good here, both server and port are correct. my ws host and game server are on the same ip. my webserver port is 8080. actually my webserver endpoint is :8080/somePath. Is that an issue?
wsHandle = Websocket_Open(wsHost, wsPort, wsIncomingCallback, wsErrCallback, wsCloseCallback); //<--- erroring line: full traceback in next block
bool ok = Websocket_Send(wsHandle, SendType_Text, "Hello world!");
}
//-------------------------------------------My Callbacks:
public Action wsIncomingCallback(WebsocketHandle parent, WebsocketHandle child, const char[] host, int port, char protocols[256], char GETPath[2000])
{
return Plugin_Continue;
}
public void wsCloseCallback(WebsocketHandle socket)
{
socket = INVALID_WEBSOCKET_HANDLE;
}
public void wsErrCallback(WebsocketHandle socket, const int errType, const int errNum)
{
}
Code:
L 09/17/2022 - 23:24:40: [SM] Exception reported: Invalid Handle 0 (error: 4)
L 09/17/2022 - 23:24:40: [SM] Blaming: websocket.smx
L 09/17/2022 - 23:24:40: [SM] Call stack trace:
L 09/17/2022 - 23:24:40: [SM] [0] GetArraySize
L 09/17/2022 - 23:24:40: [SM] [1] Line 171, C:\Users\Jannik\Documents\GitHub\sm-websocket\scripting\websocket.sp::Native_Websocket_Open
L 09/17/2022 - 23:24:40: [SM] [3] Websocket_Open
L 09/17/2022 - 23:24:40: [SM] [4] Line 458, /home/steam/steamcmd/tf2/tf/addons/sourcemod/scripting/mything.sp::OnPluginStart
If I want to send out a message, I have to hook incoming messages, store that handle somewhere, then use websocket_send to send something out.
What if I want to create an outgoing message to a foreign server at will? My main desire for websockets is for my game servers to be clients, chiefly.