简体   繁体   中英

C# communication with browser using JavaScript

How to pass messages to C# application from JavaScript, I'm able to do this with PHP and tcpListner in c#(but using PHP need server to host), i need localhost communicate c# application with browser(using javaScript or any other possible way), browser need to pass messages to application running on same matchine

can you suggest appropriate method for this with sample

You can do this in the following way.

step 1 : you have to create a Listener. TcpListener class or HttpListener in .net can be used to develop the listener. This code shows how to implement a TCP listener.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

//Author : Kanishka 
namespace ServerSocketApp
{
class Server
{
 private TcpListener tcpListn = null;
 private Thread listenThread = null;
 private bool isServerListening = false;

 public Server()
 {
  tcpListn = new TcpListener(IPAddress.Any,8090);
  listenThread = new Thread(new ThreadStart(listeningToclients));
  this.isServerListening = true;
  listenThread.Start();
 }

 //listener
 private void listeningToclients()
 {
  tcpListn.Start();
  Console.WriteLine("Server started!");
  Console.WriteLine("Waiting for clients...");
  while (this.isServerListening)
  {
   TcpClient tcpClient = tcpListn.AcceptTcpClient();
   Thread clientThread = new Thread(new ParameterizedThreadStart(handleClient));
   clientThread.Start(tcpClient);
  }

 }

 //client handler
 private void handleClient(object clientObj)
 {
  TcpClient client = (TcpClient)clientObj;
  Console.WriteLine("Client connected!");

  NetworkStream stream = client.GetStream();
  ASCIIEncoding asciiEnco = new ASCIIEncoding();

  //read data from client
  byte[] byteBuffIn = new byte[client.ReceiveBufferSize];
  int length = stream.Read(byteBuffIn, 0, client.ReceiveBufferSize);
  StringBuilder clientMessage = new StringBuilder("");
  clientMessage.Append(asciiEnco.GetString(byteBuffIn));

  //write data to client
  //byte[] byteBuffOut = asciiEnco.GetBytes("Hello client! \n"+"You said : " + clientMessage.ToString() +"\n Your ID  : " + new Random().Next());
  //stream.Write(byteBuffOut, 0, byteBuffOut.Length);
  //writing data to the client is not required in this case

  stream.Flush();
  stream.Close();
  client.Close(); //close the client
 }

 public void stopServer()
 {
  this.isServerListening = false;
  Console.WriteLine("Server stoped!");
 }

}
}

Step 2 : you can pass parameters to the created server as a GET request. You can use either JavaScript or HTML Forms to pass parameters. JavaScript libraries like jQuery and Dojo will make it easier to make ajax requests.

http://localhost:8090?id=1133

You have to modify the above code to retrieve the parameters send as a GET request. I recommend to use HttpListener instead of TcpListener

Once you have done with the listening part rest part is just processing the retrieved parameters from the request.

您应该使用HttpListener类,或者创建一个自托管的ASP.Net Web API项目。

我想你需要像Comet 这样的东西, 请使用Comet查看这个例子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM