cscの作法 その168

# 概要
cscの作法、調べてみた。
ChromeDevToolsProtocol、叩いてみた。
yahoo開いてみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/eaa6d700-5e5e-77a2-74d0-d610e7fe7f8a.png)

# コンパイル手順

```
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%


>csc chrome1.cs /reference:websocket-sharp.dll /reference:LitJSON.dll

```

# サンプルコード

```

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Diagnostics;
using WebSocketSharp;
using WebSocketSharp.Net;
using LitJson;
using System.Net;
using System.Text;
using System.Web;
using System.IO;

public class Form1 : Form {
	private WebSocket client;
	Button button0,
		button1,
		button2,
		button3,
		button4;
	string webSocketDebuggerUrl;
	public Form1() {
		this.ClientSize = new Size(400, 400);
		this.Text = "cdp";
		button0 = new Button();
		button0.Location = new Point(10, 10);
		button0.Size = new Size(80, 30);
		button0.Text = "open";
		button1 = new Button();
		button1.Location = new Point(110, 10);
		button1.Size = new Size(80, 30);
		button1.Text = "start";
		button2 = new Button();
		button2.Location = new Point(210, 10);
		button2.Size = new Size(80, 30);
		button2.Text = "set";
		button3 = new Button();
		button3.Location = new Point(10, 60);
		button3.Size = new Size(80, 30);
		button3.Text = "";
		button4 = new Button();
		button4.Location = new Point(110, 60);
		button4.Size = new Size(80, 30);
		button4.Text = "";
		Controls.Add(button0);
		Controls.Add(button1);
		Controls.Add(button2);
		Controls.Add(button3);
		Controls.Add(button4);
		button0.Click += Button0_Click;
		button1.Click += Button1_Click;
		button2.Click += Button2_Click;
		button3.Click += Button3_Click;
		button4.Click += Button4_Click;
	}
	private void Button0_Click(object sender, EventArgs e) {
		using (var process = new Process()) 
		{
			process.StartInfo = new ProcessStartInfo {
				FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
				Arguments = @"--remote-debugging-Port=9222",
				CreateNoWindow = true,
				UseShellExecute = false,
			};
			process.Start();
		}
	}
	private void Button1_Click(object sender, EventArgs e) {
		WebRequest request = WebRequest.Create("http://localhost:9222/json");
		WebResponse response = request.GetResponse();
		Console.WriteLine(((HttpWebResponse) response).StatusDescription);
		using (Stream dataStream = response.GetResponseStream())
		{
			StreamReader reader = new StreamReader(dataStream);
			string responseFromServer = reader.ReadToEnd();
			Console.WriteLine(responseFromServer);
			JsonData jsonData = JsonMapper.ToObject(responseFromServer);
			Console.WriteLine(jsonData[0]["webSocketDebuggerUrl"]);
			webSocketDebuggerUrl = (string) jsonData[0]["webSocketDebuggerUrl"];
			Console.WriteLine(webSocketDebuggerUrl);
			client = new WebSocket(webSocketDebuggerUrl);
			client.OnOpen += (ss, ee) => Console.WriteLine(string.Format("Connected : to {0} successfully ", webSocketDebuggerUrl));
			client.OnError += (ss, ee) => Console.WriteLine("Error : " + ee.Message);
			client.OnMessage += (ss, ee) => Console.WriteLine("Echo : " + ee.Data);
			client.OnClose += (ss, ee) => Console.WriteLine(string.Format("Disconnected : with {0} ", webSocketDebuggerUrl));
			client.Connect();
		}
		response.Close();
	}
	private void Button2_Click(object sender, EventArgs e) {
		string json = "{\"id\": 1, \"method\": \"Runtime.evaluate\", \"params\": {\"expression\": \"location.href='https://www.yahoo.co.jp/'\"}}";
		Console.WriteLine(json); 
		if (!string.IsNullOrEmpty(json))
			client.Send(json);
	}
	private void Button3_Click(object sender, EventArgs e) {

	}
	private void Button4_Click(object sender, EventArgs e) {

	}
	[STAThread]
	public static void Main() {
		Application.Run(new Form1());
	}
}





```
以上。