- Details
- Written by: Stanko Milosev
- Category: node.js
- Hits: 4693
One example of receiving messages for node.js. I was using nodejs-websocket module as WebSocket server.Â
My server looks like this:
var ws = require("nodejs-websocket") var server = ws.createServer(function (conn) { console.log("New connection") conn.on("close", function (code, reason) { console.log("Connection closed") }) }).listen(8081) setInterval(function () { console.log("Sending 'Test'"); try { server.connections.forEach(function (con) { console.log("Sending 'Test'"); con.sendText("Test"); }); } catch (e) { console.log('Error!'); console.log(e); } }, 1000);
Client looks like this:
window.onload = function () { function MyConnectionWrapper() { var self = this, createSocket = function () { return new WebSocket('ws://127.0.0.1:8081') }, ws = createSocket(); self.onopen = function (myFunc) { ws.onopen = myFunc; } self.onmessage = function (myFunc) { console.log('In self: ' + myFunc); ws.onmessage = myFunc; }; } myConn = new MyConnectionWrapper(); myConn.onmessage(function (myFunc) { console.log('In myConn: ' + myFunc); }); }
Here notice lines:
myConn = new MyConnectionWrapper();
myConn.onmessage(function (myFunc) {
 console.log('In myConn: ' + myFunc);
});
With first line:
myConn = new MyConnectionWrapper();
we have created socket, because automatically following code is executed:
createSocket = function () { return new WebSocket('ws://127.0.0.1:8081') },
with that myConn became WebSocket, and with self we assigned onmessage method to myConn:
self.onmessage = function (myFunc) {
 console.log('In self: ' + myFunc);
 ws.onmessage = myFunc;
};
In this case myFunc will be function which we assigned like this:
myConn.onmessage(function (myFunc) {
 console.log('In myConn: ' + myFunc);
});
Example download from here.
- Details
- Written by: Stanko Milosev
- Category: node.js
- Hits: 4788
From Node.js v0.10.31 Manual & Documentation I copied example, like:
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/');
If you save example like "example.js" and after starting server in command prompt with:
node example.js
in your browser go to the address:
http://localhost:8124/
and you should see "Hello World"
- Details
- Written by: Stanko Milosev
- Category: Software Development Philosophy
- Hits: 7879
I created new section and category, software development philosophy, I get an idea to name it like this from wikipedia's article, List of software development philosophies
Since in that list there is no refinement, I would like to mention it.
According to wikipedia, program refinement is the verifiable transformation of an abstract (high-level) formal specification into a concrete (low-level) executable program.
So, what that exactly mean?
In Delphi it look something like this:
procedure SortFile (filename:string);
begin
// Read file into memory
// Sort the lines
// Write file out of memory
end;
Later you develop your code.
I started to use this approach few months ago, and it is very usefull, when I start coding like this, usually my code look much better.
I took the code, and idea of writing all this from here.