JavaScriptからC#アプリケーションにメッセージを渡す方法は、PHPとtcpListnerを使ってC#で行うことができます。 (JavaScriptや他の可能な方法を使用して)、ブラウザが同じmatchineにJavaScriptを使用したブラウザとのC#通信
を実行しているアプリケーションにメッセージを渡す必要がありますが、あなたはあなたがHttpListener
クラスを使用するか、自己を作成する必要がありますサンプル
JavaScriptからC#アプリケーションにメッセージを渡す方法は、PHPとtcpListnerを使ってC#で行うことができます。 (JavaScriptや他の可能な方法を使用して)、ブラウザが同じmatchineにJavaScriptを使用したブラウザとのC#通信
を実行しているアプリケーションにメッセージを渡す必要がありますが、あなたはあなたがHttpListener
クラスを使用するか、自己を作成する必要がありますサンプル
あなたは、次の方法でこれを行うことができます。
ステップ1:リスナーを作成する必要があります。 TcpListenerクラスまたは.netのHttpListenerを使用してリスナーを開発することができます。このコードは、TCPリスナーを実装する方法を示しています。
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!");
}
}
}
ステップ2:あなたがGET要求として作成されたサーバにパラメータを渡すことができます。 JavaScriptまたはHTMLフォームを使用してパラメータを渡すことができます。 jQueryやDojoのようなJavaScriptライブラリーでは、ajaxリクエストを簡単に作成できます。
http://localhost:8090?id=1133
上記のコードを変更して、GET要求として送信するパラメータを取得する必要があります。 は、私はあなただけのリクエストから取得したパラメータを処理しているリスニングパートの残りの部分で行われていたらHttpListenerを代わりのTcpListener
を使用することをお勧めします。
で、このための適切な方法を提案することができますASP.Net Web APIプロジェクトをホストしました。私はあなたが彗星のようなものが必要だと思う
http://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener –
いいえ私はメッセージをC#アプリケーションに渡すためにブラウザが必要です – UdayaLakmal
それを再度読んでください。 –