WebSocket Error – Connection is already closed.

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(“request_data_1”). I had no trouble receiving the data using ws.recv(). But when I sent the next frame of data using ws.send(“request_data_2”), I got the Connection is already closed and socket is already closed error message. I couldn’t figure out why exactly the connection was getting closed.


After some research, I figured out that the server was expecting data in JSON format. I transformed the data being sent to json format and things started working smoothly.

ws.send(json.dumps("request_data_1"))

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!

If you liked this article, learn more about Qxf2’s testing services for startups.


Avinash Shetty

I am a software tester with over 10 years of experience in software testing. Currently I am working at Qxf2 Services Bangalore. As a student of the context-driven approach to software testing I feel there is a lot to learn out there which keeps me very excited. My work has helped me gain good experience in different areas of testing like CRM, Web, Mobile and Database testing. I have good knowledge of building test scripts using Automation tools like Selenium and Appium using Java and Python. Beside testing I am a “Sports Fanatic” and love watching and playing sports.

4 Comments

  1. Sivaraman said:

    Thanks. Saved my day

    June 17, 2017
    Reply
  2. Ahmed Ayman said:

    Thanks a LOT

    February 6, 2018
    Reply
  3. Anonymous said:

    My Requirement:

    I have to connect to the Websocket through JMeter Once it is connected, I have to send some API requests having JSON data as body to the server and I have to verify the JSON response of it

    Worked Items:

    I have tried using Maciej Zaleski Websocket plugin but couldnt make through I am able to connect to the websocket using Websocket open Connection but after that I am unable to send JSON data to the server Throwing an error as

    “Error: [Execution Flow] – Opening new connection – Using response message pattern “” – Using disconnect pattern “” – Waiting for the server connection for 5000 MILLISECONDS – Cannot connect to the remote server

    [Variables] – Message count: 0

    [Problems] – Unexpected error: null JMeter.plugins.functional.samplers.websocket.ServiceSocket.sendMessage(ServiceSocket.java:189) JMeter.plugins.functional.samplers.websocket.WebSocketSampler.sample(WebSocketSampler.java:141) org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) java.lang.Thread.run(Unknown Source)

    Log: 2018-08-07 14:57:54,013 INFO o.e.j.w.c.WebSocketClient: Stopped org.eclipse.jetty.websocket.client.WebSocketClient@35c6f96b 2018-08-07 14:57:54,014 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, local)

    August 20, 2018
    Reply
    • Annapoorani Gurusamy Annapoorani Gurusamy said:

      Hi ,
      You can refer these links below
      i)https://www.blazemeter.com/blog/jmeter-websocket-samplers-a-practical-guide
      ii)https://bitbucket.org/pjtr/jmeter-websocket-samplers

      August 22, 2018
      Reply

Leave a Reply

Your email address will not be published.