一、Processing的介绍
Processing是一种具有革命前瞻性、创新型计算机编程语言,为图像处理提供开源编程语言和环境,动画和互动。成千上万的视觉设计师、艺术家和建筑师使用这一编程语言来创作他们的作品。如纽约现代艺术博物馆、伦敦维多利亚和阿尔伯特博物馆、巴黎乔治·蓬皮杜中心和其他许多著名场所涉及到的项目。也可以用于为舞蹈和音乐表演创建投影舞台设计;为音乐视频和电影生成图像;为海报、杂志和书籍导出图像;以及在画廊、博物馆和街道上创建交互式体验。
![80452d5eea21a4e7f3c0abe01b643833.png](https://img-blog.csdnimg.cn/img_convert/80452d5eea21a4e7f3c0abe01b643833.png)
Processing介绍
二、Websocket的介绍
![01f61431756fab6cbcccc414ac0476f7.png](https://img-blog.csdnimg.cn/img_convert/01f61431756fab6cbcccc414ac0476f7.png)
Websocket介绍
WebSocket是一种通信协议,可在单个TCP连接上进行全双工通信。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,客户端和服务器只需要完成一次握手,两者之间就可以建立持久性的连接,并进行双向数据传输。
![0c70bd13eaa85ed92516ba528c77c8a7.png](https://img-blog.csdnimg.cn/img_convert/0c70bd13eaa85ed92516ba528c77c8a7.png)
Websocket协议图例
三、在Processing应用之间传递数据消息
本例是采Websocket的技术,在Processing服务器端与多个Processing客户端之间传递数据消息,从而实现多个Processing应用的互动。
1、Websocket客户端
//引入Websocket库import websockets.*;WebsocketClient wsc;int now;boolean newEllipse;void setup(){ size(200,200); newEllipse=true; //创建websocket客户端,并监听来自服务的消息 wsc= new WebsocketClient(this, "ws://localhost:8025/john"); now=millis();}void draw(){ if(newEllipse){ ellipse(random(width),random(height),10,10); newEllipse=false; } if(millis()>now+5000){ //发送消息给Websocket服务器端 wsc.sendMessage("Client message"); now=millis(); }}void webSocketEvent(String msg){ println(msg); newEllipse=true;}
运行效果
![bd190ee95b0d9944e09e1cf4594e3bfe.png](https://img-blog.csdnimg.cn/img_convert/bd190ee95b0d9944e09e1cf4594e3bfe.png)
Websocket客户端运行效果
2、Websocket服务器
当Websocket服务器端收到客户端的消息(“Client message”)时,服务器端会将椭圆移动到随机位置,每5秒向Websocket客户端发送一条消息(“Server message”),以下提供了Websocket客户端的完整示例代码与运行效果。
websocketserver.pde
//引入Websocket库import websockets.*;WebsocketServer ws;int now;float x,y;void setup(){ size(200,200); //创建Websocket服务器,并监听来自客户端的消息 ws= new WebsocketServer(this,8025,"/john"); now=millis(); x=0; y=0;}void draw(){ background(0); ellipse(x,y,10,10); if(millis()>now+5000){ //发送消息给客户端 ws.sendMessage("Server message"); now=millis(); }}void webSocketServerEvent(String msg){ println(msg); x=random(width); y=random(height);}
运行效果
![dfcf2bb75d62607f03f65f3eec1c7d40.png](https://img-blog.csdnimg.cn/img_convert/dfcf2bb75d62607f03f65f3eec1c7d40.png)
Websocket服务器端运行效果