Fabric.js序列化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31519989/article/details/99562022

Fabric.js序列化

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Fabric.js序列化</title>
</head>
<style>
    html, body {
        margin:0;
        padding:0;
    }
    .container {
        height:100%;
        width:100%;
        border: 1px solid green;
        position:fixed;
    }
</style>
<body>
    <canvas class="container" id="canvas"></canvas>

    <script src="lib/fabric.js"></script>
    <script>
        var canvas = new fabric.Canvas('canvas');
        var rect = new fabric.Rect({
            width: 100,
            height: 100,
            fill: 'red'
        });
        canvas.add(rect);
        console.log(JSON.stringify(canvas.toJSON()));
    </script>
</body>
</html>

在这里插入图片描述

Json数据

{"version":"3.3.0","objects":[{"type":"rect","version":"3.3.0","originX":"left","originY":"top","left":0,"top":0,"width":100,"height":100,"fill":"red","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeMiterLimit":4,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","paintFirst":"fill","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0}]}
展开阅读全文

序列化序列化

04-21

这个是客户端代码,用来将ServiceItem这个类序列化后发送给服务器端;ServiceItem.ServiceItem是引用外部的一个程序集; rn ServiceItem.ServiceItem service = new ServiceItem.ServiceItem();rn service.Address = "172.18.10.5";rn rn service.rentTime = 100;rn rn TcpClient client = new TcpClient("172.18.10.5",9062);rn NetworkStream strm = client.GetStream();rnrn object obj = service;rnrn formatter.Serialize(strm,obj);rn strm.Close();rn client.Close(); rnrn这个是服务器端代码,用来接收客户端发来的ServiceItem序列化类。服务器端没有引用ServiceItem.ServiceItem这个程序集。rnTcpListener server = new TcpListener(9062);rn server.Start();rnrn Console.WriteLine("Begin to listen...");rn TcpClient client = server.AcceptTcpClient();rn NetworkStream strm = client.GetStream();rn IFormatter formatter = new BinaryFormatter();rnrn object obj = formatter.Deserialize(strm);rn rn strm.Close();rn server.Stop();rnrn Stream str = new FileStream("C:\\Inetpub\\wwwroot\\bb", FileMode.Create, FileAccess.ReadWrite);rn formatter.Serialize(str, obj);rn rn str.Close();rn执行到object obj = formatter.Deserialize(strm);时出错,说什么找不到ServiceItem.ServiceItem程序集。我的问题是,上述代码如何改进,才可以以让服务器端不知道客户端序列化的什么类,而通通序列化后放在object obj中?貌似我这个程序必须要在服务器端添加ServiceItem.ServiceItem程序集,可是我不想。 论坛

没有更多推荐了,返回首页