3

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?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
user3479074
  • 131
  • 1
  • 3
  • 9
  • 2
    TCP/UDP sockets or REST server or SOAP server – matcheek Aug 03 '15 at 13:55
  • really what matcheek said - but others include Named Pipes, 0MQ and the other libraries that make TCP easier to write, Kafka or other durable middleware. Nobody is going to write your code for you, but if you post your example you may find people willing to help find issues in it. – akaphenom Aug 03 '15 at 14:00
  • Have a look at the the SignalR project. http://asp.net/signalr – Steve Aug 03 '15 at 14:07
  • Are websockets really necessary? Http requests would work otherwise. – Josh C. Aug 03 '15 at 16:39
  • So I tried some things the last days and was able to create a signalR-based Webserver in C# which was accessible from javascript. However it seems to be impossible to run in Unity. Does anyone know another way to get a message into the unity-application? – user3479074 Aug 08 '15 at 14:56

2 Answers2

0

Try creating a web service with WCF: https://msdn.microsoft.com/en-us/library/vstudio/bb412178(v=vs.100).aspx

Ben
  • 209
  • 2
  • 8
0

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

nomail
  • 635
  • 6
  • 21