Skip to content

Instantly share code, notes, and snippets.

@TheDutchDev
Last active October 11, 2021 12:12
Show Gist options
  • Save TheDutchDev/1e58ca5b5075354e564dd78f147ad999 to your computer and use it in GitHub Desktop.
Save TheDutchDev/1e58ca5b5075354e564dd78f147ad999 to your computer and use it in GitHub Desktop.
ALTV WebsocketSharp with LetsEncrypt SSL (https://github.com/sta/websocket-sharp)
using AltV.Net.Data;
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using WebSocketSharp;
using WebSocketSharp.Server;
public class WebSocketManager
{
private static WebSocketServer _server;
public void Run( )
{
// YourPort = any port you want to run the WS server on
_server = new WebSocketServer( YourPort, true );
// Example for LetsEncrypt SSL certificates on ubuntu
// YourSslCertificate => /etc/letsencrypt/live/yourwebsite.com/cert.pem
// YourSslKey => /etc/letsencrypt/live/yourwebsite.com/privkey.pem
X509Certificate2 cert = X509Certificate2.CreateFromPemFile( YourSslCertificate, YourSslKey );
_server.SslConfiguration.ServerCertificate = cert;
_server.SslConfiguration.ClientCertificateRequired = false; // Not entirely sure if this is required
_server.SslConfiguration.CheckCertificateRevocation = false; // Not entirely sure if this is required
_server.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12; // This is definitely required
// Not entirely sure if this is required.
_server.SslConfiguration.ClientCertificateValidationCallback = (object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors) =>
{
return true;
};
// Change this to the URL at which you want to host the websocket server, eg use "/ws" for example.com/ws
_server.AddWebSocketService<WebSocketService>( "/" );
_server.Start( );
Console.WriteLine( "[MANAGER] WebSocket Server is listening: " + _server.IsListening );
}
}
public class WebSocketService : WebSocketBehavior
{
protected override void OnMessage( MessageEventArgs e )
{
Console.WriteLine( $"WS Message: { e.Data }" );
}
protected override void OnOpen( )
{
Console.WriteLine( $"Client connected!" );
}
protected override void OnClose( CloseEventArgs e )
{
Console.WriteLine( $"Client disconnected!" );
}
protected override void OnError( ErrorEventArgs e )
{
Console.WriteLine( e.Message );
}
}
// In any other file(eg via DI, or server start method)
WebSocketManager ws = new WebSocketManager( ).Run( );
// For client-side use ALTV's built-in WebSocketClient, example below.
import * as alt from 'alt-client';
// Specify URL and port here
const wsClient = new alt.WebSocketClient( "wss://YOUR_WS_URL:YOUR_WS_PORT" );
wsClient.autoReconnect = true; // auto reconnect if it disconnects for some reason
wsClient.on( "open", ( ) => {
alt.log( "[WEBSOCKET] Connected!" );
} );
wsClient.on( "close", ( code, reason ) => {
alt.log( `[WEBSOCKET] Disconnected: ${ code }, ${ reason }` );
} );
wsClient.on( "error", ( error ) => {
alt.log( `[WEBSOCKET] Disconnected: ${ error }` );
} );
wsClient.on( "message", ( message ) => {
alt.log( `[WEBSOCKET] message: ${ message }` );
} );
wsClient.start( );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment