{"id":4526,"date":"2016-07-28T12:39:15","date_gmt":"2016-07-28T16:39:15","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=4526"},"modified":"2018-04-03T10:38:14","modified_gmt":"2018-04-03T14:38:14","slug":"websocket-error-connection-already-closed","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/websocket-error-connection-already-closed\/","title":{"rendered":"WebSocket Error &#8211; Connection is already closed."},"content":{"rendered":"<p>While trying to send a WebSocket transaction, I hit the following error<\/p>\n<div style=\"background-color: #ffff99;\">\n<strong>Connection is already closed.<br \/>\n<\/strong>\n<\/div>\n<hr>\n<p>I received this error when working on an application that involved a WebSocket connection. The Websocket handshake between the client and server was successful. After that I was able to send data to server using ws.send(&#8220;request_data_1&#8221;). I had no trouble receiving the data using ws.recv(). But when I sent the next frame of data using ws.send(&#8220;request_data_2&#8221;), I got the <em>Connection is already closed<\/em> and <em>socket is already closed<\/em> error message. I couldn&#8217;t figure out why exactly the connection was getting closed.<\/p>\n<hr>\n<p>After some research, I figured out that the server was expecting data in JSON format. I transformed the data being sent to <a href=\"http:\/\/www.w3schools.com\/json\/\">json<\/a> format and things started working smoothly.<\/p>\n<pre lang = \"python\">\r\nws.send(json.dumps(\"request_data_1\"))\r\n<\/pre>\n<hr>\n<p>When looking up this error online, I noticed Google was not very helpful. So I thought I would write this short post to help anyone else who stumbled across this error. Hope this helped!<\/p>\n<p><strong>If you liked this article, learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2&#8217;s<\/a> testing services for startups.<\/strong><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>While trying to send a WebSocket transaction, I hit the following error Connection is already closed. I received this error when working on an application that involved a WebSocket connection. The Websocket handshake between the client and server was successful. After that I was able to send data to server using ws.send(&#8220;request_data_1&#8221;). I had no trouble receiving the data using [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,112],"tags":[],"class_list":["post-4526","post","type-post","status-publish","format-standard","hentry","category-api-testing","category-websocket"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=4526"}],"version-history":[{"count":14,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4526\/revisions"}],"predecessor-version":[{"id":8426,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4526\/revisions\/8426"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=4526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=4526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=4526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}