2012-01-10 94 views
2

我正在創建一個小應用程序,該應用程序應遠程控制 瀏覽器窗口:導航,填充表單等。我試圖使用Chrome遠程調試和C#將 目標歸檔。目前原型 連接到瀏覽器,獲取可用頁面的列表並建立WebSocket連接 。Chrome DevTools:遠程DOM修改

  1. 開始的chrome.exe --remote調試端口= 9123
  2. Connecto到WS://本地主機:9123/devtools /頁面/ 1和發送:

    request: { "id": 1, "method": "DOM.setOuterHTML", "params": {"nodeId": 1, "outerHTML": ""}} 
    

它看起來錯了,但沒有返回錯誤。

代碼:

 var client = new WebClient(); 
     var pages = client.DownloadString("http://localhost:9123/json"); 
     Console.WriteLine(pages); // works well, displays JSON with info about pages 

     // create WebSocket 
     var socket = new WebSockets.WebSocket("ws://localhost:9123/devtools/page/1"); 
     // it should print messages to console: 
     socket.OnMessage += (sender, eventdata) => Console.WriteLine(eventdata); 

     // works well - sends and recieves handshaking messages 
     socket.Connect(); 

     Console.WriteLine("\nType \"exit\" to exit.\n"); 

     string data; 
     while (true) 
     { 
      Thread.Sleep(500); 
      Console.Write("> "); 
      data = Console.ReadLine(); 
      if (data == "exit") 
       break; 
      // sends data: 
      socket.Send(data); 
     } 

數據是怎樣發送到WebSocket的流:

  byte[] dataBuffer = Encoding.UTF8.GetBytes(data); 
      _wsStream.WriteByte(0x00); 
      _wsStream.Write(dataBuffer, 0, dataBuffer.Length); 
      _wsStream.WriteByte(0xff); 

,讀數不會返回:

private void Message() // started in different thread 
    { 
     while (_readyState == WsState.Open) 
     { 
      string data = Receive(); // does _wsStream.ReadByte(); and stops on it forever 
      var handler = OnMessage; 
      if (null != handler && data != null) 
       handler(this, data); 
     } 
    } 

能否請你幫我命令語法和故障排除 的問題...

感謝,

亞歷

回答

0

我建議你使用WebSocket4Net NuGet包。

using WebSocket4Net; 

//Create new socket connection 
WebSocket socket = new WebSocket(tab.webSocketDebuggerUrl); 
socket.MessageReceived += MessageReceived; 
socket.Open(); 
socket.Send("String to send"); 

private static void Ss_MessageReceived(object sender, WebSocket4Net.MessageReceivedEventArgs e) 
{ 
    //Handle here response from server 
}