Call C# dll function from TypeScript | Overwolf
Overwolf is a popular platform that allows developers to create in-game overlays and apps for various games. One common requirement for many Overwolf apps is the ability to call C# functions from TypeScript. In this blog post, we will explore different solutions to achieve this integration.
Solution 1: Using WebSockets
One way to call C# functions from TypeScript in an Overwolf app is by using WebSockets. This approach involves setting up a WebSocket server in the C# code and establishing a connection between the TypeScript code and the server.
Here’s an example of how you can implement this solution:
// TypeScript code
const socket = new WebSocket('ws://localhost:8080');
socket.onopen = () => {
console.log('WebSocket connection established');
};
socket.onmessage = (event) => {
const message = event.data;
console.log('Received message:', message);
// Handle the received message from C# code
};
// Send a message to C# code
socket.send('Hello from TypeScript!');
// C# code
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class WebSocketServer
{
private const int Port = 8080;
public static void Main()
{
var server = new TcpListener(IPAddress.Parse("127.0.0.1"), Port);
server.Start();
Console.WriteLine("WebSocket server started");
var client = server.AcceptTcpClient();
var stream = client.GetStream();
var buffer = new byte[1024];
var message = new StringBuilder();
while (stream.Read(buffer, 0, buffer.Length) != 0)
{
message.Append(Encoding.UTF8.GetString(buffer));
if (message.ToString().IndexOf('n') > -1)
{
// Handle the received message from TypeScript code
Console.WriteLine("Received message: " + message);
message.Clear();
}
}
client.Close();
server.Stop();
}
}
This solution establishes a WebSocket connection between the TypeScript code and the C# server. Messages can be sent back and forth, allowing the TypeScript code to call C# functions and receive responses.
Solution 2: Using Overwolf’s Native Messaging API
Overwolf provides a Native Messaging API that allows communication between the Overwolf app and external processes, such as C# applications. This API enables you to send and receive messages between TypeScript and C# code.
Here’s an example of how you can implement this solution:
// TypeScript code
const sendMessageToCSharp = (message) => {
overwolf.extensions.current.getNativeMessagingToken((result) => {
if (result.success) {
overwolf.extensions.current.sendNativeMessage(result.token, message, (response) => {
console.log('Received response:', response);
// Handle the response from C# code
});
}
});
};
// Call C# function
sendMessageToCSharp('Hello from TypeScript!');
// C# code
using System;
using System.IO;
public class NativeMessagingHost
{
public static void Main()
{
while (true)
{
var message = ReadMessage();
// Handle the received message from TypeScript code
Console.WriteLine("Received message: " + message);
SendMessage("Response from C# code");
}
}
private static string ReadMessage()
{
var lengthBytes = new byte[4];
var stream = Console.OpenStandardInput();
stream.Read(lengthBytes, 0, 4);
var length = BitConverter.ToInt32(lengthBytes, 0);
var messageBytes = new byte[length];
stream.Read(messageBytes, 0, length);
return System.Text.Encoding.UTF8.GetString(messageBytes);
}
private static void SendMessage(string message)
{
var length = BitConverter.GetBytes(message.Length);
Console.OpenStandardOutput().Write(length, 0, 4);
Console.OpenStandardOutput().Write(System.Text.Encoding.UTF8.GetBytes(message), 0, message.Length);
Console.OpenStandardOutput().Flush();
}
}
This solution utilizes Overwolf’s Native Messaging API to establish communication between the TypeScript and C# code. The TypeScript code sends a message to the C# code, which then handles the message and sends a response back to the TypeScript code.
Conclusion
Calling C# functions from TypeScript in an Overwolf app can be achieved using various methods. In this blog post, we explored two solutions: using WebSockets and utilizing Overwolf’s Native Messaging API. Both approaches allow seamless integration between TypeScript and C# code, enabling developers to create powerful and interactive Overwolf apps.
Remember to choose the solution that best fits your specific requirements and project needs. Happy coding!
Leave a Reply