Created
December 1, 2013 13:46
-
-
Save satomacoto/7733856 to your computer and use it in GitHub Desktop.
msgpack, unity, c#, flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import msgpack | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/l') | |
def l(): | |
""" | |
>>> import msgpack | |
>>> import requests | |
>>> msgpack.unpackb(requests.get('http://0.0.0.0:5000/l').content) | |
[1, 2, 3] | |
""" | |
return msgpack.packb([1, 2, 3]) | |
@app.route('/d') | |
def d(): | |
""" | |
>>> import msgpack | |
>>> import requests | |
>>> msgpack.unpackb(requests.get('http://0.0.0.0:5000/d').content) | |
{'foo': 'bar'} | |
""" | |
return msgpack.packb({"foo":"bar"}) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hoge | |
{ | |
public int bar {get; set;} | |
public int baz {get; set;} | |
public int foo {get; set;} | |
} | |
void msgpacksample() | |
{ | |
// hashmap-like | |
string url = "http://0.0.0.0:5000/d"; | |
WWW www = new WWW(url); | |
yield return www; | |
MessagePackSerializer<Dictionary<MessagePackObject, MessagePackObject>> argsSerializer = MessagePackSerializer.Create<Dictionary<MessagePackObject, MessagePackObject>>(); | |
Dictionary<MessagePackObject, MessagePackObject> obj = argsSerializer.UnpackSingleObject(www.bytes); | |
Debug.Log(obj["foo"].ToString()); | |
// array-like | |
url = "http://0.0.0.0:5000/l"; | |
www = new WWW(url); | |
yield return www; | |
var serializer = MessagePackSerializer.Create<Hoge>(); | |
using (var stream = new System.IO.MemoryStream(www.bytes)) | |
{ | |
var hoge = serializer.Unpack(stream); | |
Debug.Log (hoge.foo); | |
Debug.Log (hoge.bar); | |
Debug.Log (hoge.baz); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment