banner
xingli

xingli

猫娘爱好者

godotC# integration with socket.io

Integration of godotC# with socket.io#

Godot code

using Godot;
using System;
using SocketIOClient;

public partial class test : Node
{
    private SocketIO client;

    public override void _Ready()
    {
        client = new SocketIO("http://localhost:11000/");
        client.On("hi", response =>
        {
            Console.WriteLine(response);
            string text = response.GetValue<string>();
        });
        client.OnConnected += async (sender, e) =>
        {
            await client.EmitAsync("hi", "socket.io");
        };
        client.ConnectAsync();
    }
}

socketio v4 code

// Import the socket.io module
const socketIO = require('socket.io');

// Create an HTTP server
const http = require('http');
const server = http.createServer();

// Create a socket.io server
const io = socketIO(server);

// Listen for connection event
io.on('connection', (socket) => {
    console.log('Client connected:', socket.id);

    // Listen for "hi" event from the client
    socket.on('hi', (message) => {
        console.log('Received "hi" from client:', socket.id);
        console.log('Message:', message);

        // Send a message to the client
        socket.emit('hi', 'Hello from server!');
    });

    // Listen for disconnect event
    socket.on('disconnect', () => {
        console.log('Client disconnected:', socket.id);
    });
});

// Start the server and listen on the specified port
const PORT = 11000;
server.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
});

Yes, socket.io sends a string. If you want to send an object, you can convert the object to a JSON string and then send it to the server. On the server side, you can convert the JSON string back to an object.

Here's an example:

In this example, we create an anonymous object with two properties. Then, we use the JsonConvert.SerializeObject() method to convert the object to a JSON string. Finally, we use the client.EmitAsync() method to send the JSON string to the server.

If you need more information about socket.io, please let me know and I'll do my best to help you.

If you want to add a message named "identity", you can use the following code:

In this code snippet, client is a socket.io client object that connects to the server by calling the ConnectAsync() method. When the client is connected to the server, it sends a "hi" message and an "identity" message to the server.

If you need more information about socket.io, please let me know and I'll do my best to help you.

Yes, you need to call the ConnectAsync() method to connect to the server. The ConnectAsync() method is an asynchronous method that executes in the background without blocking the main thread.

If you want to know more about the ConnectAsync() method, please let me know and I'll do my best to help you.

using Godot;
using System;
using SocketIOClient;

public partial class test : Node
{
    private SocketIO client;

    public override void _Ready()
    {
        var id = new { username = "maomao" };
        client = new SocketIO("http://localhost:11000/");
        client.On("hi", response =>
        {
            // Console.WriteLine(response);
            GD.Print(response);
            string text = response.GetValue<string>();
        });
        client.OnConnected += async (sender, e) =>
        {
            await client.EmitAsync("hi", "socket.io");
            await client.EmitAsync("identify", id);
        };
        client.ConnectAsync();
    }
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.