Node.js の HTTP パッケージを使用して、socket.io の条件付きアクションを実装しようとしています。
基本的に私が欲しいのは、get リクエストによると、socket.io が別の関数を呼び出したり、別のデータを送信したりすることです。
これが私のコードです:
var http = require('http'),
fs = require('fs');
var app = http.createServer(function (request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
}).listen(1337);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('message_to_server', function(data) {
http.get("/something", function(res) {
io.sockets.emit("message_to_client",{ message: data["message"] });
console.log(data["message"]);
});
http.get("/else", function(res) {
console.log("something else");
});
});
});
そのような機能を実装するにはどうすればよいですか?