2015-06-04 7 views
9

सर्वर: एक Node.js: sails.js (0.11.x) सर्वरsails.js को Node.js सॉकेट ग्राहक के साथ एक घटना फेंकना (0.11.x)

ग्राहक है और [email protected] साथ स्क्रिप्ट [email protected]

बिग चित्र: मैं है, या होगा, Node.js लिपियों कि sails.js सर्वर से कनेक्ट की एक खेत और विभिन्न कार्यों का प्रदर्शन करेगा।

तात्कालिक लक्ष्य:

socket.emit('got_job', job.id); 

क्यों: मैं इस तरह के रूप ग्राहक> सर्वर से एक सॉकेट कनेक्शन के दौरान एक घटना फेंकना चाहते हैं? यदि यह संभव है तो मैं एक नियंत्रक (या नियंत्रक + सेवा) में सर्वर पक्ष पर विभिन्न ईवेंट हैंडलर बना सकता हूं और इस स्क्रिप्ट फार्म का समर्थन करने के लिए क्लाइंट/सर्वर एंडपॉइंट्स के बीच राज्यव्यापी लेनदेन के सेट को प्रबंधित करते समय अपना कोड साफ़ रख सकता हूं।

प्रलेखन: यह कैसे एक sails.js इस के लिए socket.io-क्लाइंट का उपयोग प्रति पाल डॉक्स के बारे में चला जाता है: https://github.com/balderdashy/sails.io.js?files=1#for-nodejs

मैं क्या उस लिंक में है के अलावा अन्य साझा करने के लिए बहुत ज्यादा नहीं कोड है, लेकिन मैं इसे यहाँ सिर्फ मामले में चस्पा करेंगे:

var socketIOClient = require('socket.io-client'); 
var sailsIOClient = require('sails.io.js'); 

// Instantiate the socket client (`io`) 
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js) 
var io = sailsIOClient(socketIOClient); 

// Set some options: 
// (you have to specify the host and port of the Sails backend when using this library from Node.js) 
io.sails.url = 'http://localhost:1337'; 
// ... 

// Send a GET request to `http://localhost:1337/hello`: 
io.socket.get('/hello', function serverResponded (body, JWR) { 
    // body === JWR.body 
    console.log('Sails responded with: ', body); 
    console.log('with headers: ', JWR.headers); 
    console.log('and with status code: ', JWR.statusCode); 

    // When you are finished with `io.socket`, or any other sockets you connect manually, 
    // you should make sure and disconnect them, e.g.: 
    io.socket.disconnect(); 

    // (note that there is no callback argument to the `.disconnect` method) 
}); 

क्या मैं में ध्यान दिया है: मैं इन वस्तुओं के विभिन्न स्तरों में drilled है और मैं उपयोग करने के लिए संपर्क में कुछ भी नहीं कर पा रहे। और बस io.socket.emit() की कोशिश कर रहा है क्योंकि यह अस्तित्व में नहीं है। लेकिन io.socket.get() और io.socket.post(), आदि ठीक काम करते हैं।

console.log(socketIOClient); 
console.log(sailsIOClient); 
console.log(io); 
console.log(io.socket._raw); 
console.log(io.sails); 

धन्यवाद, और मैं इसे स्पष्टीकरण के लिए आवश्यकतानुसार अपडेट करने का प्रयास करूंगा।

अद्यतन:

विविध सर्वर जानकारी।: अलग बंदरगाहों (3000-3003) पर

  • मैं पोर्ट 443 पर nginx उपयोग कर रहा हूँ, एसएसएल समाप्ति के साथ, (जल्द ही अधिक से ) 4 की ओर इशारा करते sails.js उदाहरणों।
  • मैं सत्र और सॉकेट के लिए रेडिस का भी उपयोग कर रहा हूं।
+0

मुझे अभी तक कोई समाधान नहीं मिला है। यह जानना अच्छा होगा कि किसी और ने इसमें भाग लिया है या नहीं। – nodnarB

+0

आप 'io.socket.disconnect();' क्यों बना रहे हैं? – Bulkin

+0

मैं इसके साथ भी संघर्ष कर रहा हूं, और मुझे एक समाधान मिल रहा है। मैं सर्वर पर एक जेसन संदेश के साथ उत्सर्जित करना और ईवेंट करना चाहता हूं। nodnarB क्या आप एक कामकाज खोजने में सक्षम थे? – zabumba

उत्तर

3

आप पास कर रहे हैं:

आपका io.socket.get कॉल तरह का आराम API कॉल की तरह है। आप एक पाल नियंत्रक यूआरएल '/ हैलो' पर GET अनुरोध करने के लिए बाध्य

//client 
io.socket.get('/hello', function serverResponded (body, JWR) { 
    //this is the response from the server, not a socket event handler 
    console.dir(body);//{message:"hello"} 
}); 

में
config/routes.js: 
{ 
'get /hello':'MyController.hello' 
} 

//controllers/MyController 
{ 
    hello:function(req,res){ 
    res.json({message:"hello"}); 
    } 
} 

विधि आप के लिए देख रहे हैं, io.socket आवश्यकता होगी।पर ('घटनानाम');

//client 
    io.socket.get('/hello', function serverResponded (body, JWR) { 
     //all we're doing now is subscribing to a room 
     console.dir(body); 
    }); 
    io.socket.on('anevent',function(message){ 
     console.dir(message); 
    }); 


in 

    config/routes.js: 
    { 
    'get /hello':'MyController.hello' 
    } 

    //controllers/MyController 
    { 
     hello:function(req,res){ 
     sails.sockets.join(req.socket,'myroom'); 
     res.json({message:'youve subscribed to a room'}); 
     } 
    } 

क्या हम प्रभावी ढंग से किया गया है, सेटअप हमारे सॉकेट एक "कमरा", जो मूल रूप से एक घटना नाम स्थान है का हिस्सा बनने का:

यहाँ एक उदाहरण है। फिर, कुछ अन्य नियंत्रक केवल यह करने के लिए दिया गया है:

sails.sockets.broadcast('myroom','anevent',{message:'socket event!'}); 

के बाद इस फोन किया है, तो आप io.socket.on ('anevent') के कॉलबैक में संदेश प्राप्त होगा।

+0

सवाल यह था कि ब्राउज़र से सर्वर पर किसी ईवेंट को कैसे निकालना है। आप जो वर्णन करते हैं, सर्वर से सभी जुड़े ग्राहकों को उत्सर्जित करना है – zabumba

संबंधित मुद्दे