gpt4 book ai didi

从C程序连接到node.js websocket服务器

转载 作者:行者123 更新时间:2023-11-30 14:54:56 25 4
gpt4 key购买 nike

我正在尝试从 C 程序或 Chrome 扩展智能 Websocket 客户端连接到用 node.js + socket.io 编写的 websocket 服务器,但我不能。我只能从其他 Nodejs 应用程序或 js + socket.io 连接到它。

Node.js 服务器:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(3000);

function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}

res.writeHead(200);
res.end(data);
});
}

io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});

C 客户端(我使用 github.com/payden/libwsclient):

#include <time.h>
#include <wsclient/wsclient.h>
#include <errno.h>


int onopen(wsclient *c) {
printf("ad\n");
fprintf(stderr, "onopen called: %d\n", c->sockfd);
libwsclient_send(c, "Hello onopen");
return 0;
}


void initSocketConnection() {
//char *host = "ws://echo.websocket.org";
char *host = "ws://127.0.0.1:3000";

wsclient *client = libwsclient_new(host);

if(!client) {
fprintf(stderr, "Unable to initialize new WS client.\n");
exit(1);
}
printf("connecrionnn \n");
libwsclient_onopen(client, &onopen);

char *msg = "{\"msg\":\"STATION_READY_FOR_ACTION\", \"appKey\":\"secret-key\"}";
libwsclient_send(client, msg);
}

我做错了什么?

最佳答案

我解决了问题,套接字端点必须是“ws://127.0.0.1:3000/socket.io/?EIO=3&transport=websocket”

关于从C程序连接到node.js websocket服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46400167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com