{"id":3441,"date":"2018-07-13T15:40:26","date_gmt":"2018-07-13T06:40:26","guid":{"rendered":"http:\/\/soundengine.jp\/wordpress\/tips\/?p=3441"},"modified":"2018-07-13T15:41:11","modified_gmt":"2018-07-13T06:41:11","slug":"c%e3%81%a7http%e3%81%aeget%e3%82%a2%e3%82%af%e3%82%bb%e3%82%b9%e3%81%a8websocket","status":"publish","type":"post","link":"https:\/\/soundengine.jp\/wordpress\/tips\/try\/3441\/","title":{"rendered":"C#\u3067HTTP\u306eGET\u30a2\u30af\u30bb\u30b9\u3068WebSocket"},"content":{"rendered":"<p>\u4eca\u65e5\u306f\u3001C#\u3067HTTP\u306eGET\u30a2\u30af\u30bb\u30b9\u3068WebSocket\u306b\u3088\u308b\u30c7\u30fc\u30bf\u53d6\u5f97\u3092\u3084\u3063\u3066\u307f\u3088\u3046\u304b\u306a\u3068\u3002<\/p>\n<p>GET\u30a2\u30af\u30bb\u30b9\u306b\u306f\u3001WebClient\u3092\u4f7f\u7528\u3002WebSocket\u306b\u306f\u3001WebSocket4Net(NuGet\u304b\u3089\u30d1\u30c3\u30b1\u30fc\u30b8\u53d6\u5f97)\u3092\u4f7f\u7528\u3057\u305f\u3002\u304a\u307e\u3051\u3067\u3001Newtonsoft.Json\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3082\u5229\u7528\u3057\u3066\u3044\u308b\u3002<\/p>\n<p><!--more--><\/p>\n<h2>HTTP\u306eGET\u30a2\u30af\u30bb\u30b9<\/h2>\n<p>\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u3089WebClient\u3067JSON\u3092\u53d6\u5f97\u3059\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true \">\r\nprivate void button1_Click(object sender, EventArgs e)\r\n{\r\n\tvar webClient = new WebClient();     \r\n\tvar stopwatch = new Stopwatch();\r\n\tstopwatch.Start();\r\n\r\n\twebClient.DownloadStringAsync( new Uri(\"https:\/\/api.bitflyer.jp\/\/v1\/getticker?product_code=BTC_JPY\") );\r\n\twebClient.DownloadStringCompleted += ( s1, e1 ) =&gt;\r\n\t{\r\n\t\tstopwatch.Stop();\r\n\t\ttextBox1.Text = DateTime.Now + Environment.NewLine + e1.Result + Environment.NewLine + string.Format(\"{0} ms\", stopwatch.ElapsedMilliseconds ) + Environment.NewLine + \"----------------------------\" + Environment.NewLine + textBox1.Text;\r\n\t};\r\n}\r\n<\/pre>\n<p>1\u56de\u76ee\u306e\u30a2\u30af\u30bb\u30b9\u306f\u6642\u9593\u304b\u304b\u308b\u304c2\u56de\u76ee\u304b\u3089\u306f\u901f\u3044\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_1.png\" alt=\"\" width=\"707\" height=\"508\" class=\"alignnone size-full wp-image-3456\" srcset=\"https:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_1.png 707w, https:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_1-300x216.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/p>\n<p>Environment.NewLine\u3068\u3044\u3046\u306e\u306fWindows\u3060\u3068CRLF\u306b\u306a\u308b\u3088\u3046\u3060\u3002\u6700\u521d\u3001&#8221;\\n&#8221;\u3068\u304b\u66f8\u3044\u3066\u6539\u884c\u3055\u308c\u306a\u3044\u306e\u3067\u5c11\u3005\u7126\u3063\u305f\u3068\u3053\u308d\u3060\u3063\u305f\u3002<\/p>\n<h2>WebSocket<\/h2>\n<p>C#\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u306eWebSocket\u306f\u4f7f\u3044\u306b\u304f\u3044\u3089\u3057\u304f\u3001WebSocket4Net\u3068\u3044\u3046\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u3057\u305f\u3002C#\u306eJSON\u3082\u540c\u69d8\u306b\u4f7f\u3044\u306b\u304f\u3044\u306e\u3060\u308d\u3046\u304b\uff1f\u3068\u3044\u3046\u3053\u3068\u3067Newtonsoft.Json\u3092\u4f7f\u3063\u305f\u3002<\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<pre class=\"lang:c# decode:true \">\r\nprivate void button2_Click(object sender, EventArgs e)\r\n{\r\n\tvar channelName = \"lightning_board_snapshot_BTC_JPY\";\r\n\r\n\tvar websocket = new WebSocket(\"wss:\/\/ws.lightstream.bitflyer.com\/json-rpc\", sslProtocols: SslProtocols.Tls12);\r\n\r\n\twebsocket.Opened += (s1, e1) =&gt;\r\n\t{\r\n\t\twebsocket.Send(\r\n\t\t\tJsonConvert.SerializeObject(\r\n\t\t\t\tnew\r\n\t\t\t\t{\r\n\t\t\t\t\tmethod = \"subscribe\",\r\n\t\t\t\t\t@params = new { channel = channelName },\r\n\t\t\t\t\tid = 256,\r\n\t\t\t\t}\r\n\t\t\t)\r\n\t\t);\r\n\t};\r\n\twebsocket.MessageReceived += (s1, e1) =&gt;\r\n\t{\r\n\t\tdynamic data = JObject.Parse(e1.Message);\r\n\t\tif (data.id == 256)\r\n\t\t{\r\n\t\t\tDebug.WriteLine(\"subscribed!\");\r\n\t\t}\r\n\t\tif (data.@params != null)\r\n\t\t{\r\n\t\t\tthis.BeginInvoke((Action)(() =&gt; { textBox1.Text = DateTime.Now + Environment.NewLine + data.@params.message + Environment.NewLine + \"----------------------------\" + Environment.NewLine + textBox1.Text; }));\r\n\t\t}\r\n\t};\r\n\r\n\twebsocket.Open();       \r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_2.png\" alt=\"\" width=\"707\" height=\"508\" class=\"alignnone size-full wp-image-3458\" srcset=\"https:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_2.png 707w, https:\/\/soundengine.jp\/wordpress\/tips\/files\/2018\/07\/20180713_2-300x216.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/p>\n<p>\u306f\u307e\u3063\u3066\u3057\u307e\u3063\u305f\u70b9\u306f\u3001\u307e\u305a\u306f@params\u304c\u306a\u3093\u306a\u306e\u304b\u3068\u3044\u3046\u3068\u3053\u308d(\u4e88\u7d04\u8a9e\u56de\u907f\u306e@\u3089\u3057\u3044)\u3002\u3042\u3068\u306f\u3001data.@params.message\u3092\u305d\u306e\u307e\u307eDebug.WriteLine\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3068\u3053\u308d\u30fb\u30fb\u30fb\u3002<\/p>\n<blockquote><p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: &#8216;\u6761\u4ef6\u4ed8\u304d\u5c5e\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u30e1\u30bd\u30c3\u30c9 &#8216;WriteLine&#8217; \u3092\u52d5\u7684\u306b\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093&#8217;<\/p><\/blockquote>\n<p>\u3053\u308c\u304c\u4f55\u3092\u8868\u3057\u3066\u3044\u308b\u306e\u304b\u3001\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u3051\u3069\u3001\u52d5\u7684\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066WriteLine\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u304c\u5408\u308f\u306a\u3044\u611f\u3058\u306a\u3093\u3060\u308d\u3046\u304b\uff1f<\/p>\n<pre class=\"lang:c# decode:true \">Debug.WriteLine((object)data.@params.message);\r\nDebug.WriteLine((string)data.@params.message.ToString());<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u306b\u3059\u308c\u3070\u554f\u984c\u306a\u304f\u52d5\u304f\u3063\u307d\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u65e5\u306f\u3001C#\u3067HTTP\u306eGET\u30a2\u30af\u30bb\u30b9\u3068WebSocket\u306b\u3088\u308b\u30c7\u30fc\u30bf\u53d6\u5f97\u3092\u3084\u3063 &hellip; <a href=\"https:\/\/soundengine.jp\/wordpress\/tips\/try\/3441\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[33,34],"class_list":["post-3441","post","type-post","status-publish","format-standard","hentry","category-try","tag-c","tag-34"],"_links":{"self":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/3441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/comments?post=3441"}],"version-history":[{"count":3,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/3441\/revisions"}],"predecessor-version":[{"id":3459,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/3441\/revisions\/3459"}],"wp:attachment":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/media?parent=3441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/categories?post=3441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/tags?post=3441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}