UNPKG

crn-cli

Version:

The Ctrip React Native CLI Tools

1 lines 6.19 kB
__d(function(g,r,i,a,m,e,d){'use strict';var t=r(d[0]),n=r(d[1]),s=r(d[2]),o=r(d[3]),l=r(d[4]),c=r(d[5]),u=r(d[6]),h=u.Image,b=u.PixelRatio,f=u.ScrollView,p=u.StyleSheet,y=u.Text,S=u.TextInput,E=u.TouchableOpacity,v=u.View,k='ws://localhost:5555/',w='http://localhost:5556/',C=['close','error','message','open'],T=['CONNECTING','OPEN','CLOSING','CLOSED'],x=(function(u){function h(){return t(this,h),s(this,o(h).apply(this,arguments))}return l(h,u),n(h,[{key:"render",value:function(){var t=c.createElement(y,{style:U.buttonLabel},this.props.label);return this.props.disabled?c.createElement(v,{style:[U.button,U.disabledButton]},t):c.createElement(E,{onPress:this.props.onPress,style:U.button},t)}}]),h})(c.Component),_=(function(u){function h(){return t(this,h),s(this,o(h).apply(this,arguments))}return l(h,u),n(h,[{key:"render",value:function(){return c.createElement(v,{style:U.row},c.createElement(y,null,this.props.label),this.props.value?c.createElement(y,null,this.props.value):null,this.props.children)}}]),h})(c.Component),P=(function(u){function b(){var n,l;t(this,b);for(var c=arguments.length,u=new Array(c),h=0;h<c;h++)u[h]=arguments[h];return(l=s(this,(n=o(b)).call.apply(n,[this].concat(u)))).ws=null,l.state={blob:null},l}return l(b,u),n(b,[{key:"componentDidMount",value:function(){var t=this,n=this.ws=new WebSocket(this.props.url);n.binaryType='blob',n.onmessage=function(n){if(n.data instanceof Blob){var s=n.data;t.state.blob&&t.state.blob.close(),t.setState({blob:s})}},n.onopen=function(t){n.send('getImage')}}},{key:"componentUnmount",value:function(){this.state.blob&&this.state.blob.close(),this.ws&&this.ws.close()}},{key:"render",value:function(){return this.state.blob?c.createElement(h,{source:{uri:URL.createObjectURL(this.state.blob)},style:{width:50,height:50}}):c.createElement(v,null)}}]),b})(c.Component);function A(t){return void 0===t||null===t?'(no value)':'undefined'!=typeof ArrayBuffer&&'undefined'!=typeof Uint8Array&&t instanceof ArrayBuffer?"ArrayBuffer {"+String(Array.from(new Uint8Array(t)))+"}":t}var L=(function(u){function h(){var n,l;t(this,h);for(var c=arguments.length,u=new Array(c),b=0;b<c;b++)u[b]=arguments[b];return(l=s(this,(n=o(h)).call.apply(n,[this].concat(u)))).state={url:k,httpUrl:w,fetchStatus:null,socket:null,socketState:null,lastSocketEvent:null,lastMessage:null,outgoingMessage:''},l._connect=function(){var t=new WebSocket(l.state.url);C.forEach(function(n){return t.addEventListener(n,l._onSocketEvent)}),l.setState({socket:t,socketState:t.readyState})},l._disconnect=function(){l.state.socket&&l.state.socket.close()},l._onSocketEvent=function(t){var n={socketState:t.target.readyState,lastSocketEvent:t.type};'message'===t.type&&(n.lastMessage=t.data),l.setState(n)},l._sendText=function(){l.state.socket&&(l.state.socket.send(l.state.outgoingMessage),l.setState({outgoingMessage:''}))},l._sendHttp=function(){l.setState({fetchStatus:'fetching'}),fetch(l.state.httpUrl).then(function(t){t.status>=200&&t.status<400&&l.setState({fetchStatus:'OK'})})},l._sendBinary=function(){if(l.state.socket&&'undefined'!=typeof ArrayBuffer&&'undefined'!=typeof Uint8Array){for(var t=l.state.outgoingMessage,n=new Uint8Array(t.length),s=0;s<t.length;s++)n[s]=t.charCodeAt(s);l.state.socket.send(n),l.setState({outgoingMessage:''})}},l}return l(h,u),n(h,[{key:"render",value:function(){var t=this,n=T[this.state.socketState||-1],s=!this.state.socket||this.state.socket.readyState>=WebSocket.CLOSING,o='OPEN'===n;return c.createElement(f,{style:U.container},c.createElement(v,{style:U.note},c.createElement(y,null,"To start the WS test server:"),c.createElement(y,{style:U.monospace},"./RNTester/js/websocket_test_server.js")),c.createElement(_,{label:"Current WebSocket state",value:A(n)}),c.createElement(_,{label:"Last WebSocket event",value:A(this.state.lastSocketEvent)}),c.createElement(_,{label:"Last message received",value:A(this.state.lastMessage)}),c.createElement(_,{label:"Last image received"},o?c.createElement(P,{url:this.state.url}):null),c.createElement(S,{style:U.textInput,autoCorrect:!1,placeholder:"Server URL...",onChangeText:function(n){return t.setState({url:n})},value:this.state.url}),c.createElement(v,{style:U.buttonRow},c.createElement(x,{onPress:this._connect,label:"Connect",disabled:!s}),c.createElement(x,{onPress:this._disconnect,label:"Disconnect",disabled:s})),c.createElement(S,{style:U.textInput,autoCorrect:!1,placeholder:"Type message here...",onChangeText:function(n){return t.setState({outgoingMessage:n})},value:this.state.outgoingMessage}),c.createElement(v,{style:U.buttonRow},c.createElement(x,{onPress:this._sendText,label:"Send as text",disabled:!o}),c.createElement(x,{onPress:this._sendBinary,label:"Send as binary",disabled:!o})),c.createElement(v,{style:U.note},c.createElement(y,null,"To start the HTTP test server:"),c.createElement(y,{style:U.monospace},"./RNTester/js/http_test_server.js")),c.createElement(S,{style:U.textInput,autoCorrect:!1,placeholder:"HTTP URL...",onChangeText:function(n){return t.setState({httpUrl:n})},value:this.state.httpUrl}),c.createElement(v,{style:U.buttonRow},c.createElement(x,{onPress:this._sendHttp,label:"Send HTTP request to set cookie",disabled:'fetching'===this.state.fetchStatus})),c.createElement(v,{style:U.note},c.createElement(y,null,'OK'===this.state.fetchStatus?'Done. Check your WS server console to see if the next WS requests include the cookie (should be "wstest=OK")':'-')))}}]),h})(c.Component),U=p.create({container:{flex:1},note:{padding:8,margin:4,backgroundColor:'white'},monospace:{fontFamily:'courier',fontSize:11},row:{height:40,padding:4,backgroundColor:'white',flexDirection:'row',justifyContent:'space-between',alignItems:'center',borderBottomWidth:1/b.get(),borderColor:'grey'},button:{margin:8,padding:8,borderRadius:4,backgroundColor:'blue',alignSelf:'center'},disabledButton:{opacity:.5},buttonLabel:{color:'white'},buttonRow:{flexDirection:'row',justifyContent:'center'},textInput:{height:40,backgroundColor:'white',margin:8,padding:8}});e.title='WebSocket',e.description='WebSocket API',e.examples=[{title:'Basic websocket',render:function(){return c.createElement(L,null)}}]},666795,[3,4,5,8,9,11,15]);