How to proxy to multiple servers in WebSocket - websocket

I have a proxy programmed in ballerina that receives a request from a user to a web socket and sends it through another web socket to a service that processes it. Currently when the service sends me a response, the ballerina script returns that response to the user. I would like to do that once I receive the response from the service, it is sent to another service to perform a second processing and when this service responds and return it to the user.
I have the following code:
import ballerina/http;
import ballerina/log;
final string ASSOCIATED_CONNECTION = "EXTRACTOR CONNECTION";
final string EXTRACTOR = "ws://192.168.10.248:8081";
#http:WebSocketServiceConfig {
path: "/api/ws"
}
service RequestService on new http:Listener(9091) {
resource function onOpen(http:WebSocketCaller caller) {
http:WebSocketClient wsClientEp = new(
EXTRACTOR,
{callbackService: ClientService,
readyOnConnect: false,
maxFrameSize: 2147483648
});
wsClientEp.setAttribute(ASSOCIATED_CONNECTION, caller);
caller.setAttribute(ASSOCIATED_CONNECTION, wsClientEp);
var err = wsClientEp->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client", err);
}
}
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var e = clientEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hence closing the connection", err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode, string reason) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
}
service ClientService = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var e = serverEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
function getAssociatedClientEndpoint(http:WebSocketCaller ep) returns (http:WebSocketClient) {
http:WebSocketClient wsClient = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsClient;
}
function getAssociatedServerEndpoint(http:WebSocketClient ep) returns (http:WebSocketCaller) {
http:WebSocketCaller wsEndpoint = <http:WebSocketCaller>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
I have made the following pipeline:
Client --> Ballerina Proxy --> Service1 --> Ballerina Proxy --> Client
I want:
Client --> Ballerina Proxy --> Service1 --> Ballerina Proxy --> Service2 --> Ballerina Proxy --> Client

We can chain the associated connections
caller has wsClientEp
wsClientEp has wsClientEp2
wsClientEp2 has caller
I have written some rough code I think the following code should work. Make sure to change the variable names:
import ballerina/http;
import ballerina/log;
final string ASSOCIATED_CONNECTION = "EXTRACTOR CONNECTION";
final string EXTRACTOR = "ws://localhost:9090/basic";
final string EXTRACTOR2 = "ws://localhost:9091/basic";
#http:WebSocketServiceConfig {
path: "/api/ws"
}
service RequestService on new http:Listener(9092) {
resource function onOpen(http:WebSocketCaller caller) {
http:WebSocketClient wsClientEp = new(
EXTRACTOR,
{callbackService: ClientService1,
readyOnConnect: false,
maxFrameSize: 2147483648
});
http:WebSocketClient wsClientEp2 = new(
EXTRACTOR2,
{callbackService: ClientService2,
readyOnConnect: false,
maxFrameSize: 2147483648
});
caller.setAttribute(ASSOCIATED_CONNECTION, wsClientEp);
wsClientEp.setAttribute(ASSOCIATED_CONNECTION, wsClientEp2);
wsClientEp2.setAttribute(ASSOCIATED_CONNECTION, caller);
var err = wsClientEp->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 1", err);
}
err = wsClientEp2->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 2", err);
}
}
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var e = clientEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hence closing the connection", err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode, string reason) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
}
service ClientService1 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var e = clientEp2->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
service ClientService2 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var e = serverEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
function getAssociatedClientEndpoint(http:WebSocketCaller ep) returns (http:WebSocketClient) {
http:WebSocketClient wsClient = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsClient;
}
function getAssociatedServerEndpoint(http:WebSocketClient ep) returns (http:WebSocketCaller) {
http:WebSocketCaller wsEndpoint = <http:WebSocketCaller>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
function getAssociatedClientEndpointFromClient(http:WebSocketClient ep) returns (http:WebSocketClient) {
http:WebSocketClient wsEndpoint = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
Note that I have change the urls a bit for testing.

Related

Ballerina: Multiple websocket error on callback

I've programmed a proxy server using ballerina. This proxy receives requests from customers through a websocket. The goal is that the Ballerina server collects this request and sends it to the server called "EXTRACTOR". This server processes the request and returns a response to the Ballerina server. At that time the Ballerina server does not return the response to the client who made the request, but sends the response of the "EXTRACTOR" to another server called "EXTRACTOR2" that will process the response of "EXTRACTOR". Later "EXTRACTOR2" returns the processed information to Ballerina's server and this one returns it to the client that made the request in the first instance. All the communication between Ballerina, the clients, EXTRACTOR and EXTRACTOR2 is done through websockets. The problem lies in the fact that when EXTRACTOR2 tries to return the information to Ballerina's server, it finds that it cannot because the communication has been closed.
This is my code:
import ballerina/http;
import ballerina/log;
final string ASSOCIATED_CONNECTION = "EXTRACTOR CONNECTION";
final string EXTRACTOR = "ws://localhost:9090/basic";
final string EXTRACTOR2 = "ws://localhost:9091/basic";
#http:WebSocketServiceConfig {
path: "/api/ws"
}
service RequestService on new http:Listener(9092) {
resource function onOpen(http:WebSocketCaller caller) {
http:WebSocketClient wsClientEp = new(
EXTRACTOR,
{callbackService: ClientService1,
readyOnConnect: false,
maxFrameSize: 2147483648
});
http:WebSocketClient wsClientEp2 = new(
EXTRACTOR2,
{callbackService: ClientService2,
readyOnConnect: false,
maxFrameSize: 2147483648
});
caller.setAttribute(ASSOCIATED_CONNECTION, wsClientEp);
wsClientEp.setAttribute(ASSOCIATED_CONNECTION, wsClientEp2);
wsClientEp2.setAttribute(ASSOCIATED_CONNECTION, caller);
var err = wsClientEp->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 1", err);
}
err = wsClientEp2->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 2", err);
}
}
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var e = clientEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hence closing the connection", err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode, string reason) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
}
service ClientService1 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var e = clientEp2->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
service ClientService2 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var e = serverEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
function getAssociatedClientEndpoint(http:WebSocketCaller ep) returns (http:WebSocketClient) {
http:WebSocketClient wsClient = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsClient;
}
function getAssociatedServerEndpoint(http:WebSocketClient ep) returns (http:WebSocketCaller) {
http:WebSocketCaller wsEndpoint = <http:WebSocketCaller>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
function getAssociatedClientEndpointFromClient(http:WebSocketClient ep) returns (http:WebSocketClient) {
http:WebSocketClient wsEndpoint = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
When EXTRACTOR2 tries to return the information to Ballerina's server this error pops up:
Error in connection handler
Traceback (most recent call last):
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 795, in transfer_data
message = await self.read_message()
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 863, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 938, in read_data_frame
frame = await self.read_frame(max_size)
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 1018, in read_frame
extensions=self.extensions,
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/framing.py", line 121, in read
data = await reader(2)
File "/usr/lib/python3.7/asyncio/streams.py", line 677, in readexactly
raise IncompleteReadError(incomplete, n)
asyncio.streams.IncompleteReadError: 0 bytes read on a total of 2 expected bytes
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/server.py", line 195, in handler
await self.ws_handler(self, path)
File "analyzer_main.py", line 39, in server
await websocket.send(json.dumps(response))
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 530, in send
await self.ensure_open()
File "/home/cluster/.local/lib/python3.7/site-packages/websockets/protocol.py", line 771, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
Exception found
{"ok": 0, "data": "[ANALYZER] <class 'websockets.exceptions.ConnectionClosedError'>code = 1006 (connection closed abnormally [internal]), no reason"}
This is the python EXTRACTOR2 code:
import json
import asyncio
import websockets
from analyzer import Analyzer
analyzer = Analyzer()
print("Ready")
async def server(websocket, path):
try:
request_json = await websocket.recv()
#await websocket.send(":)")
request = json.loads(request_json)
print("Peticion recibida")
print(request_json)
with open("log.txt","w+") as f:
f.write(request_json)
# Parsear request
data = []
for topic in request['data']:
sentiment = {}
sentiment['word'] = topic['word']
sentiment['sentiment'] = analyzer.api_get_sentiment(topic['context'])
data.append(sentiment)
print("Analizado " + topic['word'])
# Enviar respuesta
response = {"ok": 1, "data": data}
print(response)
await websocket.send(json.dumps(response))
except BaseException as exception:
print("Exception found")
response = {"ok": 0, "data": "[ANALYZER] " + str(type(exception)) + str(exception)}
print(json.dumps(response))
await websocket.send(json.dumps(response))
finally:
print("Finished")
start_server = websockets.serve(server, "0.0.0.0", 8082)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
I used the following Ballerina proxy:
import ballerina/http;
import ballerina/log;
final string ASSOCIATED_CONNECTION = "EXTRACTOR CONNECTION";
final string EXTRACTOR = "ws://localhost:9091/basic";
final string EXTRACTOR2 = "ws://localhost:8082";
#http:WebSocketServiceConfig {
path: "/api/ws"
}
service RequestService on new http:Listener(9092) {
resource function onOpen(http:WebSocketCaller caller) {
http:WebSocketClient wsClientEp = new(
EXTRACTOR,
{callbackService: ClientService1,
readyOnConnect: false,
maxFrameSize: 2147483648
});
http:WebSocketClient wsClientEp2 = new(
EXTRACTOR2,
{callbackService: ClientService2,
readyOnConnect: false,
maxFrameSize: 2147483648
});
caller.setAttribute(ASSOCIATED_CONNECTION, wsClientEp);
wsClientEp.setAttribute(ASSOCIATED_CONNECTION, wsClientEp2);
wsClientEp2.setAttribute(ASSOCIATED_CONNECTION, caller);
var err = wsClientEp->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 1", err);
}
err = wsClientEp2->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client 2", err);
}
}
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var e = clientEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hence closing the connection", err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode, string reason) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
}
service ClientService1 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var e = clientEp2->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketClient clientEp2 = getAssociatedClientEndpointFromClient(caller);
var err = clientEp2->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
service ClientService2 = #http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var e = serverEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
function getAssociatedClientEndpoint(http:WebSocketCaller ep) returns (http:WebSocketClient) {
http:WebSocketClient wsClient = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsClient;
}
function getAssociatedServerEndpoint(http:WebSocketClient ep) returns (http:WebSocketCaller) {
http:WebSocketCaller wsEndpoint = <http:WebSocketCaller>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
function getAssociatedClientEndpointFromClient(http:WebSocketClient ep) returns (http:WebSocketClient) {
http:WebSocketClient wsEndpoint = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
and this python server which is a slightly modified for my testing.
import json
import asyncio
import websockets
print("Ready")
async def server(websocket, path):
try:
request_json = await websocket.recv()
print("Peticion recibida")
data = ["hello"]
response = {"ok": 1, "data": data}
print(response)
await websocket.send(json.dumps(response))
except BaseException as exception:
print("Exception found")
response = {"ok": 0, "data": "[ANALYZER] " + str(type(exception)) + str(exception)}
print(json.dumps(response))
await websocket.send(json.dumps(response))
finally:
print("Finished")
start_server = websockets.serve(server, "0.0.0.0", 8082)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
These work fine for me.

WebsocketServer: send message on event to connected client

Problem is simple: i have WS server that doing it's "for {}". I need "register" connected user, then send message on event(outside of CoreWS function). I'm added NodeJS example that doing exactly what i need, for better understanding.
Go Lang Code that fails:
i'm using this pkg https://github.com/gobwas/ws
var conn_itf interface{} //yeah,
var op_itf interface{} //i know it's not how it works
func main() {
go collector()
go sender()
http.ListenAndServe(":3333", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
// handle error
}
go func() {
defer conn.Close()
for {
msg, op, err := wsutil.ReadClientData(conn)
conn_itf = conn // i'm trying to "register" user
op_itf = op
if err != nil {
}
err = wsutil.WriteServerMessage(conn, op, msg)
if err != nil {
}
}
}()
}))
}
func collector() {
for {
fmt.Println("conn: ", conn_itf)
fmt.Println("op: ", op_itf)
time.Sleep(10000 * time.Millisecond)
}
}
func sender() {
for {
msg := []byte("Hello world!")
time.Sleep(50000 * time.Millisecond)
wsutil.WriteServerMessage(conn_itf, op_itf, msg) //invoking ws-send
}
}
NodeJS that working:
const wss = new WebSocket.Server({ port: 3333 })
wss.on('connection', ws => {
sockets.set('key', ws) //this code remember active connection
ws.on('message', message => {
console.log(`Received message => ${message}`)
})
})
function callsend(data) {
const ws = sockets.get('key') //this code sending message on invoking
ws.send(data)
}

WSDL SOAP Go Golang

I am new to Go (golang) and I am doing a test for a SOAP-CLIENT but I am not successful, I am implementing the following code but I do not receive any data:.
Install:
go get github.com/tiaguinho/gosoap
My code:
package main
import (
"log"
"github.com/tiaguinho/gosoap"
)
type LoginResponse struct {
LoginResult LoginResult
}
type LoginResult struct {
sid string
}
var (
r LoginResponse
)
func main() {
soap, err := gosoap.SoapClient("https://demo.ilias.de/webservice/soap/server.php?wsdl")
if err != nil {
log.Fatal("error not expected: ", err)
}
params := gosoap.Params{
"client": "asd",
"username": "asd",
"password": "asd",
}
err = soap.Call("login", params)
if err != nil {
log.Fatal("error in soap call: ", err)
}
soap.Unmarshal(&r)
if r.LoginResult.sid != "USA" {
log.Fatal("error: ", r)
}
}
My Error:
2018/09/10 06:48:38 error in soap call: expected element type <Envelope> but have <br>
exit status 1
I do not know how to solve the problem, Thanks for your time and help

Chat and web socket not connecting

I'm implementing chat with html5 web sockets in go lang and having errors both at client and server end.
Client Error:
WebSocket connectiont to 'ws://192.168.16.90:4000` failed: Invalid HTTP version string GET
Server Error:
continous looping of the following log message in the terminal,
connection closed\n received message:\n message sent:\n
Server Code:
func (s *Service) InitializeService() {
listener, err := net.Listen("tcp", ":4000")
if err != nil {
log.Fatal(err)
}
log.Println("Chat server started to listen on port ", listener.Addr())
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
fmt.Println("A new connection accepted.")
go listenConnection(conn)
}
}
func listenConnection(conn net.Conn) {
for {
buffer := make([]byte, 1400)
dataSize, err := conn.Read(buffer)
if err != nil {
fmt.Println("connection closed")
}
data := buffer[:dataSize]
fmt.Println("received message: ", string(data))
_, err = conn.Write(data)
if err != nil {
log.Fatalln(err)
}
fmt.Println("message sent: ", string(data))
}
}
Client Code:
var connection = new WebSocket("ws://192.168.16.90:4000");
connection.onopen = function() {
console.log("onopen");
connection.send("Ping");
};
connection.onerror = function(error) {
console.log("WebSocket Error", error);
};
connection.onmessage = function(e) {
console.log("Server: ", e.data);
};

Not getting Location header from Golang http request

I'm trying to port over something I had written in Node, the request looks like this in Node(JS):
function _initialConnection(user, pass, callback) {
let opts = {
url: config.loginURL,
headers: {
"User-Agent": "niantic"
}
};
request.get(opts, (err, resp, body) => {
if (err) return callback(err, null);
console.log(resp.headers);
let data;
try {
data = JSON.parse(body);
} catch(err) {
return callback(err, null);
}
return callback(null, user, pass, data);
});
}
function _postConnection(user, pass, data, callback) {
let opts = {
url: config.loginURL,
form: {
'lt': data.lt,
'execution': data.execution,
'_eventId': 'submit',
'username': user,
'password': pass
},
headers: {
'User-Agent': 'niantic'
}
};
request.post(opts, (err, resp, body) => {
if (err) return callback(err, null);
let parsedBody;
if (body) {
try {
parsedBody = JSON.parse(body)
if (('errors' in parsedBody) && parsedBody.errors.length > 0) {
return callback(
new Error('Error Logging In: ' + paredBody.errors[0]),
null
)
}
} catch(err) {
return callback(err, null);
}
}
console.log(resp.headers)
let ticket = resp.headers['location'].split('ticket=')[1];
callback(null, ticket);
});
}
If I console.log(resp.headers) I can see a location header.
I have tried to recreate this in Go the best way I could which I ended up with:
// Initiate HTTP Client / Cookie JAR
jar, err := cookiejar.New(nil)
if err != nil {
return "", fmt.Errorf("Failed to create new cookiejar for client")
}
newClient := &http.Client{Jar: jar, Timeout: 5 * time.Second}
// First Request
req, err := http.NewRequest("GET", loginURL, nil)
if err != nil {
return "", fmt.Errorf("Failed to authenticate with Google\n Details: \n\n Username: %s\n Password: %s\n AuthType: %s\n", details.Username, details.Password, details.AuthType)
}
req.Header.Set("User-Agent", "niantic")
resp, err := newClient.Do(req)
if err != nil {
return "", fmt.Errorf("Failed to send intial handshake: %v", err)
}
respJSON := make(map[string]string)
err = json.NewDecoder(resp.Body).Decode(&respJSON)
if err != nil {
return "", fmt.Errorf("Failed to decode JSON Body: %v", err)
}
resp.Body.Close()
// Second Request
form := url.Values{}
form.Add("lt", respJSON["lt"])
form.Add("execution", respJSON["execution"])
form.Add("_eventId", "submit")
form.Add("username", details.Username)
form.Add("password", details.Password)
req, err = http.NewRequest("POST", loginURL, strings.NewReader(form.Encode()))
if err != nil {
return "", fmt.Errorf("Failed to send second request authing with PTC: %v", err)
}
req.Header.Set("User-Agent", "niantic")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err = newClient.Do(req)
if err != nil {
return "", fmt.Errorf("Failed to send second request authing with PTC: %v", err)
}
log.Println(resp.Location())
ticket := resp.Header.Get("Location")
if strings.Contains(ticket, "ticket") {
ticket = strings.Split(ticket, "ticket=")[1]
} else {
return "", fmt.Errorf("Failed could not get the Ticket from the second request\n")
}
resp.Body.Close()
But when I log.Println(resp.Location()) I get <nil>
I'm really not sure what the differences are here (I've tried with and without the Content-Type header but for some reason I just can NOT get the Location header that I'm looking for.
I really can't see a discrepancy between the Node request, vs the Go request but any help would be great as I have been beating my head off the wall for the last day. Thanks.
You can get that url by checking resp.Request.URL after the resp, err = newClient.Do(req). There isn't a really simple way to ignore redirects, you can do a manual call through a http.RoundTripper, or you can set a CheckRedirect function on the client, but it's not ideal.

Resources