0
C 프로그램이나 Chrome 확장 스마트 웹 소켓 클라이언트에서 node.js + socket.io에 작성된 websocket 서버에 연결하려고하고 있지만 그럴 수 없습니다. 다른 nodejs 응용 프로그램 또는 js + socket.io에서만 연결할 수 있습니다.C 프로그램에서 node.js websocket 서버에 연결하십시오.
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);
}
을 뭐하는 거지?
당신은 어떤 오류가 있습니까 – nilobarp
오류가 없습니다. Chrome 확장 프로그램에서 연결할 수없는 이유는 무엇인가요? C 프로그램이 ws : //echo.websocket.org에 연결하여 localhost에 연결합니다. – user7213585