Implementing Your Own Custom Socket Connection Class

You can implement your own source code to make socket connections to the GenRocket Real Time Server in any language that supports sockets.


Example Groovy Socket Client

The following source code connects to the GenRocket Real Time Server In Groovy.

 

public static Map executeOverSocket(String path,
String accessKey,
String scenario,
String scenarioDomain,
Boolean inMemory = true,
Integer loopCount = 0,
String host = 'localhost',
Integer port = 4444,
String salesforceProfileName = null) {
   
  def map = [
      path: path,
      scenario: scenario,
      scenarioDomain: scenarioDomain,
      inMemory: inMemory,
      loopCount: loopCount,
      accessKey: accessKey,
      sfProfileName : salesforceProfileName
    ]

    def json = JsonOutput.toJson(map)
    def socket = new Socket(host, port)
    def slurper = new JsonSlurper()
    def data = null

    socket.withStreams { input, output ->
      output << json + '\n'
      data = slurper.parseText(input.text)
    }

    return data
}

   

Example Python Socket Client

The following source code connects to the GenRocket Real Time Server In Python.

  

import socket
import json

def executeOverSocket(scenario_path,
access_key,
scenario,
scenario_domain,
in_memory = True,
loop_count = 0,
host = 'localhost',
port = 4444,
salesforce_profile_name = None):
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (host, port)
    client_socket.connect(server_address)

    try:
        dict = {
            "path": scenario_path,
            "scenario": scenario,
            "scenarioDomain": scenario_domain,
            "inMemory": in_memory,
            "loopCount": loop_count,
            "accessKey": access_key,
            "sfProfileName": salesforce_profile_name
        }

        parameters = json.dumps(dict)
        parameters += '\n'

        client_socket.sendall(parameters.encode('utf-8'))    
        data = client_socket.recv(2048)  
        output = ''

        while data != "":
            output += data
            data = client_socket.recv(2048)
        
        return output
    finally:
       client_socket.close()

       

Example PHP Socket Client

The following source code connects to the GenRocket Real Time Server In PHP.

   

<?php

class SocketClient {
    public function __construct() {}
    public function executeOverSocket($path,
        $accessKey,
        $scenario,
        $scenarioDomain,
        $inMemory = true,
        $loopCount = 0,
        $host = 'localhost',
        $port = 4444,
        $salesforceProfileName = null) 
     {
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        $result = socket_connect($socket, $host, $port);

        $buffer = [
            "path" => $path,
            "scenario" => $scenario,
            "scenarioDomain" => $scenarioDomain,
            "inMemory" => $inMemory,
            "loopCount" => $loopCount,
            "accessKey" => $accessKey,
            "sfProfileName" => $salesforceProfileName
        ];

        $json = json_encode($buffer) . "\n";
        $result = socket_write($socket, $json, strlen($json));
        $output = '';

        while ($out = socket_read($socket, 2048)) {
            $output .= $out;
        }

        socket_close($socket);
        return $output;
    }
}