Thursday, December 29, 2016

I made a Server Program in Go.

MiddleMan: A Pub/Sub and Request/Response server in Go.

This is my first Go project. It is a rewrite of an existing Python server, based on some previous work: https://entitycrisis.blogspot.de/2016/09/python3-asyncio-pubsub-plaything.html

All code available here: https://github.com/simonwittber/middleman

I can see Go becoming my preferred language for game servers.

Friday, September 23, 2016

Python3 Asyncio PubSub Plaything


#!/usr/bin/env python
import io
import asyncio
import websockets
import logging
import collections

logger = logging.getLogger('websockets.server')
logger.setLevel(logging.ERROR)
logger.addHandler(logging.StreamHandler())
events = collections.defaultdict(lambda: set())
#-----------------------------------------------------------------------
async def handle_outgoing_queue(websocket):
    while websocket.open:
        msg = await websocket.outbox.get()
        await websocket.send(msg)
#-----------------------------------------------------------------------
async def pubsub(websocket, path):
    websocket.prefix = path.encode()
    websocket.outbox = asyncio.Queue()
    websocket.subscriptions = set()
    sender_task = asyncio.ensure_future(handle_outgoing_queue(websocket))
    while True:
        msg = await websocket.recv()
        if msg is None: break
        if isinstance(msg, str): msg = msg.encode()
        stream = io.BytesIO(msg)
        await handle_message(websocket, stream)
    sender_task.cancel()
    for name in websocket.subscriptions:
        try:
            events[name].remove(websocket)
        except KeyError:
            pass
#-----------------------------------------------------------------------
async def handle_message(websocket, stream):
    cmd = stream.readline().strip()
    name = websocket.prefix + stream.readline().strip()
    print(cmd, name);
    if cmd == b"SUB":
        events[name].add(websocket)
        websocket.subscriptions.add(name)
    elif cmd == b"UNS":
        subscribers = events[name]
        try:
            websocket.subscriptions.remove(name)
        except KeyError:
            pass
        try:
            subscribers.remove(websocket)
        except KeyError:
            pass
    elif cmd == b"PUB":
        stream.seek(0)
        msg = stream.read()
        for subscriber in events[name]:
            await subscriber.outbox.put(msg)
#-----------------------------------------------------------------------
start_server = websockets.serve(pubsub, '0.0.0.0', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Sunday, January 03, 2016

Notes on DragAndDrop in the Unity Editor

The documentation for the Unity DragAndDrop class is a little lacking, so here are the notes from my experiments into how the whole things works and integrates with OnGUI.


* If the current event in your OnGUI function is MouseDrag, first call:

DragAndDrop.PrepareStartDrag ()

then assign a value to DragAndDrop.objectReferences or DragAndDrop.paths and then call:

DragAndDrop.StartDrag(string title)

* If the current event is DragUpdated, you need to check if the drag operation can succeed or fail, then set:

DragAndDrop.visualMode = DragAndDropVisualMode.*

to an appropriate value. Important:If the drag operation could be accepted at this point, call:

DragAndDrop.AcceptDrag()

which will allow the DragPerform event to occur correctly if the mouse button is released.

* If the current event is DragPerform, the drag operation has succeeded and you can perform whatever logic is needed. No further DragAndDrop calls need to be made.

* If the current event is DragExited, you know that the drag operation has ended and you can perform any cleanup needed. Note: This event always occurs, even when DragPerform has completed.




Popular Posts