Hi,
Here is an example, with unix sockets:
https://github.com/tailhook/zenith/blob/master/config/zerogw.yaml
You should easily adapt it to TCP, if you know how zeromq addresses look like.
And here is also some (unfinished) tutorial:
https://github.com/tailhook/zenith/blob/master/draft.rst
--
Paul
Server:
listen:
- host: 0.0.0.0
port: 8080
Routing:
websocket:
enabled: yes
forward: !zmq.Pub
- !zmq.Connect "tcp://127.0.0.1:5000" ------->(i guess error is here)
subscribe: !zmq.Sub
- !zmq.Connect "tcp://127.0.0.1:5001" ------->(i guess error is here too)
import zmq
import random
import sys
import time
port = "5001"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
while True:
topic = random.randrange(9999,10005)
messagedata = random.randrange(1,215) - 80
print "%d %d" % (topic, messagedata)
socket.send("%d %d" % (topic, messagedata))
time.sleep(1)
import zmq
import time
import sys
port = "5000"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:%s" % port)
while True:
# Wait for next request from client
message = socket.recv()
print "Received request: ", message
time.sleep (1)
socket.send("Data from Replier")
Sorry, but you can't use REP socket for websocket connections. Use pub-sub or push-pull (with respective other counterpart). You are currently trying to publish from zerogw to REP socket in python
--
Paul
The short answer is no. The long answer is below.If so, is there any way to do it with zerogw?
Yes. Just make request reply on top of pub-sub. I.e. we usually send request with the json like:
["object.method", 1234, "arg1", "arg2"]
And reply like this:
["_reply", 1234, "response"]
So client matches response for client by id (1234 in this case).
In python code it looks like:
cli, _msg, json_data = pull_socket.recv_multipart()
meth, id, *args = json.loads(json_data)
assert _msg == 'message', _msg
obj_name, meth_name = meth.split('.')
value = getattr(objects[obj_name], meth_name)(*args)
pub_socket.send_multipart(['send', cli, json.dumps(["_reply", id, value])])
--
Paul
Server:
listen:
- host: 0.0.0.0
port: 8080
Routing:
websocket:
enabled:
yes
forward: !zmq.Push
publisher.py
------------------
import zmq
import random
import sys
import
time
pullPort = "5001"
pubPort = "5002"
context = zmq.Context()
pull_socket = context.socket(zmq.PULL)
pub_socket = context.socket(zmq.PUB)
pull_socket.bind("tcp://*:%s" % pullPort)
pub_socket.bind("tcp://*:%s" % pubPort)
while True:
cli, *argv = pull_socket.recv_multipart()
print ("incomming: ", argv)
for arg in argv:
if (arg.decode('utf-8') == "connect"):
pub_socket.send_multipart([b'send', cli, b'hello, new client'])
elif (arg.decode('utf-8') == "message"):
pub_socket.send_multipart([b'send', cli, b'reply'])
#some subscriber, that works in a separated thread
pub_socket.send_multipart([b'sendall', b'some event to all subscribers'])
#include <zmq.h>
#include <pthread.h>
#define BUFF_SIZE 1024
void *context;
void *publisher;
void *puller;
void *doPublish()
{
while (1) {
zmq_send (publisher, "sendall", 7, ZMQ_SNDMORE);
zmq_send (publisher, "some event to all subscribers", 29, 0);
sleep(2);
}
}
void *doReply()
{
while (1) {
char type[BUFF_SIZE];
char data[BUFF_SIZE];
zmq_recv (puller, type, BUFF_SIZE, ZMQ_RCVMORE);
zmq_recv (puller, data, BUFF_SIZE, 0);
printf("Type: %s\nData: %s\n\n", type, data);
// Need to get cli somehow and send reply to him
}
}
int main (void)
{
context = zmq_ctx_new ();
publisher = zmq_socket (context, ZMQ_PUB);
puller = zmq_socket (context, ZMQ_PULL);
zmq_bind (publisher, "tcp://*:5002");
zmq_bind (puller, "tcp://*:5001");
int major, minor, patch;
zmq_version(&major, &minor, &patch);
printf ("Current ØMQ version is %d.%d.%d\n", major, minor, patch);
pthread_t threads[2];
pthread_create(&threads[0], NULL, doPublish, (void*)NULL);
pthread_create(&threads[1], NULL, doReply , (void*)NULL);
pthread_join( threads[0], NULL);
pthread_join( threads[1], NULL);
// We never get here, but clean up anyhow
zmq_close (publisher);
zmq_ctx_destroy (context);
pthread_exit(NULL);
return 0;
}
void *doReply()
{
while (1) {
int64_t more;
size_t more_size = sizeof(more);
int part = 0;
char content[3][BUFF_SIZE];
do {
zmq_recv (puller, &content[part], BUFF_SIZE, 0);
zmq_getsockopt (puller, ZMQ_RCVMORE, &more, &more_size);
printf("Received part %i: %s of length %d\n", part, content[part], strlen(content[part])); // length of client is 1...
part++;
} while (more);
printf("Receiving finished\n\n\n");
if (strstr(content[1], "message") != NULL) {
printf("Trying to send reply...\n\n\n");
zmq_send(publisher, "send", strlen("send"), ZMQ_SNDMORE); // works well, displaying Websocket got SEND request
zmq_send(publisher, content[0], strlen(content[0]), ZMQ_SNDMORE); // error here, Searching for hole 1970479212...
zmq_send(publisher, "reply", strlen("reply"), 0);
}
}
}