0
URL에서 가져온 데이터를 기반으로 사용자에게 메시지를 브로드 캐스팅하려고합니다. URL에서 반환 된 데이터는 json이됩니다. 나는 node.js와 socket.io를 처음 접했고 node.js와 socket.io를 설치했다. URL을 통해 클라이언트에 브로드 캐스팅해야하는 데이터를 얻는 방법을 100 % 확신 할 수는 없습니다. 다른 요구 사항에는 무엇이 필요합니까?
내 서버 파일 server.js
var http = require('http'),
io = require('socket.io'),
// Do I need another require here?
server = http.createServer(function(req, res){
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});
//res.end('<h1>Hello world</h1>');
});
server.listen(8080);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
//I'm not sure where/how to get data from a URL
function getMessages() {
var request = messageClient.request("GET", "json.php", {"host": "myurl.com"});
// Is this how I send the data to the client?
socket.broadcast(request);
//request.end();
}
setInterval(getMessages, 1000);
client.on('message', function(){ … })
client.on('disconnect', function(){ … })
});
내 클라이언트
// Load jQuery too
<script src="http://pathtojquery.js"></script>
<script src="http://{node_server_url}/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket({node_server_url});
socket.connect();
socket.on('connect', function(){ … })
socket.on('message', function(data){
// Use jquery to handle the json data here and display message to the clients
// I can handle this part as long as 'data' is json
})
socket.on('disconnect', function(){ … })
</script>