milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Function like message handler

Details
Written by: Stanko Milosev
Category: node.js
Published: 25 November 2014
Last Updated: 25 November 2014
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.

node.js "Hello World" example

Details
Written by: Stanko Milosev
Category: node.js
Published: 25 August 2014
Last Updated: 27 November 2015
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"

WSS

Details
Written by: Stanko Milosev
Category: node.js
Published: 30 November -0001
Last Updated: 23 July 2015
Hits: 1438

WSS stands for encrypted websocket connection. 

 

Program refinement

Details
Written by: Stanko Milosev
Category: Software Development Philosophy
Published: 02 January 2009
Last Updated: 30 November -0001
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.

  1. From the RemObject
  2. REST
  3. XML DML (Data Modification Language) - SQL Server
  4. Generate XML with attributes

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 76 of 164

  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80