Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone point me to a Javascript tutorial that would allow me to create a simple Server/Client model communicating together via Named pipes?

Thanks in advance!
Posted
Comments
bbirajdar 27-Feb-13 12:43pm    
Javascript will execute within the browser. It will not have a concept of server.....
Daroosh 27-Feb-13 13:28pm    
Yes i know, i need to create a client within the browser that would communicate with a server sending/receiving info. and the connection should be a named pipe.

I managed to create a Client in Javascript which opens a communication channel with a local server (implemented in C)via using Namedpipes.

The following is how to open a NamedPipe in plain javascript:
JavaScript
// Create ActiveX Object
var object = new ActiveXObject("Scripting.FileSystemObject");
// Establish a Named Pipe Connection
var file = object.CreateTextFile("\\\\.\\pipe\\myPipeName", false);

The server should be listening to any newly created Namedpipe which carries the specific name \\\\.\\pipe\\myPipeName

In order to Write Data (In STRING Format) to the connection, you use the following piece of code:
JavaScript
var request = "Message to be Written to PIPE";
file.WriteLine( request );


As you can see, this method enables you to only send and receive data in STRING format since javascript doesn't allow full utilization of raw data (data in Raw format, Bytes, Words, Binary Format). However, when required to manipulate raw form data, i suggest you take a look at Javascript Typed Arrays. They don't always function the way you'd like them to! One thing i am still trying to figure out is, how the client can read Messages being sent from the server.

I am pretty sure this isn't the best way to do it, however it worked for me.

Any better suggestions are always welcomed! Enjoy!
 
Share this answer
 
The above mentioned solution works only for IE, since both Chrome and Firefox don't support ActiveX Objects as most of you might have already known. This following method however, can be used in Firefox to open Namedpipe connection with a local service:
JavaScript
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("\\\\.\\pipe\\myPipeName");


The following displays how to read and write to this stream:
JavaScript
// WRITE
var text = "Some text to be written";
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                   .createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file, 0x02 | 0x08, -1, 0);
outputStream.write(text, text.length);
outputStream.flush();
outputStream.close();
		
// READ
Components.utils.import("resource://gre/modules/NetUtil.jsm");
NetUtil.asyncFetch(file, function(inputStream, status) {
	if (!Components.isSuccessCode(status)) {
		// Handle error!
		return;
	}
		 
// The file data is contained within inputStream.
// You can read it into a string with
var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900