How to connect javascript and c#

4.6k Views Asked by At

I need to connect a javascript/jquery-based webpage (which is executed locally only) using a c# application.

I guess the easiest ways are using a database (what seems to be problematically to connect with from javascript) or a tcp-socket. I tried to create a socket but for some reason I'm stuck. However I was able to create a server running with node.js which sends a "hello world" to c#, but I'm not able to connect to that server with javascript.

My particular use case is a webpage to recognize gestures of a leap motion. The C# part will be run in Unity, which is not able to execute the given javascript files. So the communication between javascript and C# is not used to build the website (which would be the main reason to use asp.net, correct?)

WCF sounds interesting but is there a way to connect with it from javascript? My "server" was created with socket.io:

var net = require('net');

var server = net.createServer(function(socket){
    console.log("function called: " + socket.value);
    socket.write('hello\n');
    socket.end('world\n');
});
server.listen(8000);

I can connect to this server from my c# application, but I don't know how to send a message from javascript. When trying to connect using WebSocket I get the following error:

WebSocket connection to 'ws://127.0.0.1:8000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

using io.connect results in:

net::ERR_NAME_NOT_RESOLVED

How do I get by those errors to talk to the web application from client-side javascript?

2

There are 2 best solutions below

0
Ben On
0
nomail On

ASP.Net is the thing that you're looking for. It was built for c# and web http://www.asp.net/mvc/overview

If you need two way connection, then take a look at SignalR