Monday, 17 April 2017

non-browser version/port of simple websocket client

Say I have code like this:

<html><head>
<script type="text/javascript">
var conn = new WebSocket('wss://host/resource_path');
var max_msgs = 2;

conn.onopen = function(e){
console.log('onopen', e);
conn.send(JSON.stringify({"subscribe": "/test"}));
};

conn.onmessage = function(e){
console.log('onmessage', e.data);
if (max_msgs == 0) {
    conn.send(JSON.stringify({"unsubscribe": "/test"}));
}
else if (max_msgs < 0) {
    return;
}
max_msgs = max_msgs - 1;
};
</script>
</head>
<body>Open JS Console to see data.</body>
</html>

I got that working in a browser against our websocket server/(streaming) API. The above code is a generalized example.

I'm trying to make a non-browser/UI version for functional and especially performance testing purposes (before going to JMeter or Gatling, etc.). I couldn't figure out how to get it working right outside browser. The code I adapted from examples online doesn't work (no output / hung for node.js, or no expected onmessage output from python client).

Any tips would be great. I tried https://github.com/socketio/socket.io-client#nodejs-server-side-usage (although most examples online seem to show a client that's browser side, which doesn't help me), and also https://pypi.python.org/pypi/websocket-client/ (the long-lived connection example). I just want a barebones as easy as pie websocket CLI client that does same as what the browser version does.



via David

No comments:

Post a Comment