Http 汤,瓦拉上的网袋

Http 汤,瓦拉上的网袋,http,web,websocket,vala,libsoup,Http,Web,Websocket,Vala,Libsoup,我为测试编写了一个客户端和服务器。客户: static Soup.WebsocketConnection websocket; int main (string[] args) { var ws_session = new Soup.Session(); var ws_message = new Soup.Message("GET", "http://127.0.0.1:8088/"); string[] protocols = {"chat"}; var w

我为测试编写了一个客户端和服务器。客户:

static Soup.WebsocketConnection websocket;

int main (string[] args)
{
    var ws_session = new Soup.Session();
    var ws_message = new Soup.Message("GET", "http://127.0.0.1:8088/");

    string[] protocols = {"chat"};
    var ws_loop = new MainLoop();
    ws_session.websocket_connect_async.begin(ws_message, "localhost", protocols, null, (obj, res) => {
        websocket = ws_session.websocket_connect_async.end(res);
        websocket.message.connect(rec_message);
    });
    ws_loop.run();

    stdout.printf("Program end. Press ENTER"); // the program does not reach this point
    stdin.read_line();
    websocket.close(0, null);
    return 0;
}

void rec_message(int type, Bytes message){
    stdout.printf("WebSock receive:\n");
    stdout.printf("%s\n".printf((string)message.get_data()));
    websocket.send_text("test_message2"); // client does not send message
}
和服务器:

public class WSServer : Soup.Server {
    private int access_counter = 0;
    public WSServer(){
        assert (this != null);
        string[] protocols = {"chat"};
        this.add_websocket_handler(null,"localhost", protocols, get_ws);
    }

    void get_ws(Soup.Server server, Soup.WebsocketConnection connection, string path, Soup.ClientContext client){
        connection.message.connect(ws_message);
        string host = client.get_host();
        info (@"Client connected! Host: $host");
        string msg = """test_message1""";
        info (@"Sending to client message: $msg");
        connection.send_text(msg);
    }

    void ws_message(int id, Bytes msg){
        string message = (string)msg.get_data();
        info(@"Message received! ID: $id Message:\n$message\n");
    }
}

int main (string[] args)
{
    try {
        int port = 8088;
        MainLoop loop = new MainLoop ();
        WSServer server = new WSServer ();
        server.listen_local (port, 0);
        loop.run ();
    } catch (Error e) {
        error ("Error: %s\n", e.message);
    }

    stdout.printf("Programm end. Press ENTER");
    stdin.read_line ();
    return 0;
}

启动服务器和客户端后,第一条消息的连接和交换发生
test_message1
,之后服务器关闭连接,不再接收消息。客户端尝试发送一条消息
test_message2
,然后关闭与该代码的连接,错误消息:
WS error 44:错误接收数据:对等方重置连接
通常,我的问题的解决方案如下所示:

  • 通过删除
    ws_loop.quit(),解决了第一个问题
  • 由于实例
    WebsocketConnection
    在退出函数
    void get_ws(Soup.server,Soup.WebsocketConnection,string path,Soup.ClientContext client)时被破坏,服务器将关闭连接。

  • 一旦建立了连接,您可能希望获得
    IOStream
    。然后您可以使用和方便地通过连接进行通信。这可能是一个良好的开端point@AlThomas我使用Soup来配置、维护和处理流。它有一个WebsocketConnection类,提供消息接收信号和发送函数。我假设您正在使用?@AlexB yes我添加了一个示例,该示例不完整(rec_message在哪里?),您几乎肯定不想调用
    loop.quit()在这里,您在打开连接后立即退出程序
    
    var ws_loop = new MainLoop();
    ws_session.websocket_connect_async.begin(ws_message, "localhost", protocols, null, (obj, res) => {
        websocket = ws_session.websocket_connect_async.end(res);
        websocket.message.connect(rec_message);
        //ws_loop.quit(); // this error
    });
    ws_loop.run();