aardio - sunny库webSocketClient演示

参考在线测试网址:http://www.websocket-test.com/

上代码:

import console; 
import godking.sunny

// 同步接收
var ws = godking.sunny.webSocketClient()
ws.open( "ws://121.40.165.18:8800" )
console.dump('同步接收:\n',ws.get(),'\n')
console.dump('同步接收:\n',ws.get(),'\n')

ws.send( "这是从客户端发送的同步数据" )
console.dump('同步接收:\n',ws.get(),'\n')
ws.close()

//异步接收
var ws = godking.sunny.webSocketClient();
ws.callback = function(owner,msgType,data,dataType){
	import console
	console.dump('异步接收:\n',data,'\n')
}
ws.open( "ws://121.40.165.18:8800" )
ws.send( "这是从客户端发送的异步数据" )
//ws.close() //这里不能立即关闭,否则异步接收出错。

console.pause(true);

看截图:

aardio - sunny库webSocketClient演示_第1张图片

代码二

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="连接";left=640;top=20;right=730;bottom=60;z=1};
button2={cls="button";text="发送";left=540;top=420;right=630;bottom=460;z=5};
edit={cls="edit";text="ws://121.40.165.18:8800";left=30;top=20;right=630;bottom=60;edge=1;multiline=1;z=2};
edit2={cls="edit";text="";left=30;top=70;right=630;bottom=280;edge=1;multiline=1;z=3};
edit3={cls="edit";text="";left=30;top=290;right=630;bottom=410;edge=1;multiline=1;z=4}
)
/*}}*/

import godking.sunny
var ws = godking.sunny.webSocketClient();
ws.callback = function(winform,msgType,data,dataType){
	winform.edit2.dump( data);
}
ws.callbackOwner = winform;

winform.button.oncommand = function(id,event){
	if ws.open(winform.edit.text) winform.edit2.log( "连接成功",'\r\n' );
	else winform.edit2.log( "连接失败",'\r\n' );
}

winform.button2.oncommand = function(id,event){
	if ws.send( winform.edit3.text ) winform.edit2.log( "发送成功",'\r\n' );
	else winform.edit2.log( "发送失败",'\r\n' );
}

winform.show();
win.loopMessage();

运行效果:

aardio - sunny库webSocketClient演示_第2张图片

你可能感兴趣的:(aardio,aardio)