pusher-client-node을 통해 소비되는 데이터 스트림을 Socket.IO를 사용하여 클라이언트로 보내려고합니다.Socket.io - 푸시 스트림에서 클라이언트로 데이터 보내기
이 같은 Node.js를 내 데이터를 수신하고 있습니다 : 지속적으로 제공
var API_KEY = 'cb65d0a7a72cd94adf1f';
var pusher = new Pusher(API_KEY, {
encrypted: true
});
var channel = pusher.subscribe("ticker.160");
channel.bind("message", function (data) {
//console.log(data);
});
내 데이터는 그 다음과 같습니다
내 Socket.IO 코드는 다음과 같습니다
{ channel: 'ticker.160',
trade:
{ timestamp: 1420031543,
datetime: '2014-12-31 08:12:23 EST',
marketid: '160',
topsell: { price: '0.00007650', quantity: '106.26697381' }}
:
/**
* Socket.io
*/
var io = require("socket.io").listen(server, {log: true});
var users = [];
var stream = channel.bind("message", function (data) {
console.log(data);
});
io.on("connection", function (socket) {
// The user it's added to the array if it doesn't exist
if(users.indexOf(socket.id) === -1) {
users.push(socket.id);
}
// Log
logConnectedUsers();
socket.emit('someevent', { attr: 'value' })
stream.on("newdata", function(data) {
// only broadcast when users are online
if(users.length > 0) {
// This emits the signal to the user that started
// the stream
socket.emit('someevent', { attr: 'value' })
}
else {
// If there are no users connected we destroy the stream.
// Why would we keep it running for nobody?
stream.destroy();
stream = null;
}
});
// This handles when a user is disconnected
socket.on("disconnect", function(o) {
// find the user in the array
var index = users.indexOf(socket.id);
if(index != -1) {
// Eliminates the user from the array
users.splice(index, 1);
}
logConnectedUsers();
});
});
// A log function for debugging purposes
function logConnectedUsers() {
console.log("============= CONNECTED USERS ==============");
console.log("== :: " + users.length);
console.log("============================================");
}
나는 Node.JS. 내 푸셔 스트림을 사용하기 Node.js를하고 Socket.IO와 투쟁에 아주 새로운 오전 따라서 내 질문 : 어떻게 내 Pusher 코드 내 Socket.IO 코드를 연결하려면? 당신은 socket/io rooms를 사용할 필요가
당신이 (참조 푸셔 노드 라이브러리 https://www.npmjs.com/package/pusher)는 Pusher에게 정보를 게시하기위한 것이며 데이터를 소비하는 것이 아닙니다. "다음과 같은 노드에 데이터를 수신하고 있습니다."다음에 제공 한 코드는 참조한 라이브러리에 유효하지 않습니다. 이 코드는 Pusher JS 라이브러리를 사용할 때 웹 브라우저에서만 작동합니다. https://github.com/pusher/pusher-js – leggetter
@leggetter Thx! 그러나 socket.io 연결을 시도 할 때와 동일한 nodeJS 인스턴스에서 푸셔 스크립트를 실행 중이고 데이터를 지속적으로 수신합니다 ... – mrquad
질문에서 참조한 Node.js 라이브러리 (https : //www.npmjs. co.kr/package/pusher)가 잘못 되었습니까? 다음 라이브러리는 질문에 표시되는 API를 제공합니다. https://github.com/dirkbonhomme/pusher-client-node – leggetter