आपके कोड और टिप्पणियों के आधार पर, यह एक सुपर सरल उदाहरण है कि यह एक साथ कैसे काम करेगा।
पहले, http-server.js
- एक ठेठ एक्सप्रेस अनुप्रयोग, सिवाय इसके कि हम app.listen()
साथ सर्वर प्रारंभ नहीं है:
'use strict';
let fs = require('fs');
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
app.use(bodyParser.json());
// Let's create the regular HTTP request and response
app.get('/', function(req, res) {
console.log('Get index');
fs.createReadStream('./index.html')
.pipe(res);
});
app.post('/', function(req, res) {
let message = req.body.message;
console.log('Regular POST message: ', message);
return res.json({
answer: 42
});
});
module.exports = app;
अब
, ws-server.js
उदाहरण के लिए, जहां हम एक से WSS सर्वर बनाने नोड देशी http.createServer()
। अब, ध्यान दें कि यह वह जगह है जहां हम ऐप आयात करते हैं, और इस देशी http.create सर्वर को उपयोग करने के लिए ऐप उदाहरण दें।
PORT=8080 node ws-server.js
साथ अनुप्रयोग शुरू करें:
'use strict';
let WSServer = require('ws').Server;
let server = require('http').createServer();
let app = require('./http-server');
// Create web socket server on top of a regular http server
let wss = new WSServer({
server: server
});
// Also mount the app here
server.on('request', app);
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log(`received: ${message}`);
ws.send(JSON.stringify({
answer: 42
}));
});
});
server.listen(process.env.PORT, function() {
console.log(`http/ws server listening on ${process.env.PORT}`);
});
अंत में, इस नमूना index.html
दोनों एक पोस्ट और एक सॉकेट "अनुरोध" बनाने के द्वारा काम करेंगे और प्रतिक्रिया प्रदर्शित:
<html>
<head>
<title>WS example</title>
</head>
<body>
<h2>Socket message response: </h2>
<pre id="response"></pre>
<hr/>
<h2>POST message response: </h2>
<pre id="post-response"></pre>
<script>
// Extremely simplified here, no error handling or anything
document.body.onload = function() {
'use strict';
// First the socket requesta
function socketExample() {
console.log('Creating socket');
let socket = new WebSocket('ws://localhost:8080/');
socket.onopen = function() {
console.log('Socket open.');
socket.send(JSON.stringify({message: 'What is the meaning of life, the universe and everything?'}));
console.log('Message sent.')
};
socket.onmessage = function(message) {
console.log('Socket server message', message);
let data = JSON.parse(message.data);
document.getElementById('response').innerHTML = JSON.stringify(data, null, 2);
};
}
// Now the simple POST demo
function postExample() {
console.log('Creating regular POST message');
fetch('/', {
method: 'post',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify({message: 'What is the meaning of post-life, the universe and everything?'})
})
.then(response => response.json())
.then(function (data) {
console.log('POST response:', data);
document.getElementById('post-response').innerHTML = JSON.stringify(data, null, 2);
})
.catch(function (error) {
console.log('Request failed', error);
});
}
// Call them both;
socketExample();
postExample();
}
</script>
</body>
</html>
नोट आपको एक हालिया ब्राउज़र की आवश्यकता होगी, जिसमें एक क्लाइंट साइड भाग के लिए वेबसॉकेट और लाने वाले एपीआई दोनों हों, लेकिन यह अप्रासंगिक है, फिर भी यह आपको इसकी जानकारी देता है।
आपने कुछ लाइनें छोड़ीं। आपने एक्सप्रेस ऐप पर वेबस्केट सर्वर को 'माउंट' नहीं किया था? – Zlatko
ओह, और अपने प्रश्न का उत्तर देने के लिए। यह सामान्य तरीका है, यह सुरुचिपूर्ण क्यों नहीं है? (कुछ) की आवश्यकता होती है; myServer.mount (कुछ); मॉड्यूल किसी अन्य की तरह। – Zlatko
मैंने वास्तव में एक्सप्रेस ऐप पर WebSocketServer नहीं लगाया क्योंकि मुझे नहीं पता कि यह कैसे करें। क्या आप एक उदाहरण प्रदान करने के लिए बहुत दयालु होंगे? सहायता के लिए धन्यवाद! – Mornor