1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| <template> <scroller> <div> <div style="background-color: #286090"> <text class="title" style="height: 80px ;padding: 20px;color: white">websocket</text> </div> <input type="text" placeholder="please input message to send" class="input" autofocus="false" value="" @change="onchange" @input="oninput" ref="input"/> <div style="flex-direction: row; justify-content: center;"> <text class="button" @click="connect">connect</text> <text class="button" @click="send">send</text> <text class="button" @click="close">close</text> </div> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = send</text> </div> <text style="color: black;height: 80px">{{sendinfo}}</text> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = onopen</text> </div> <text style="color: black;height: 80px">{{onopeninfo}}</text> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = onmessage</text> </div> <text style="color: black;height: 400px">{{onmessage}}</text> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = onclose</text> </div> <text style="color: black;height: 80px">{{oncloseinfo}}</text> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = onerror</text> </div> <text style="color: black;height: 80px">{{onerrorinfo}}</text> <div style="background-color: lightgray"> <text class="title" style="height: 80px ;padding: 20px;color: black">method = close</text> </div> <text style="color: black;height: 80px">{{closeinfo}}</text> </div> </scroller> </template> <style scoped> .input { font-size: 40px; height: 80px; width: 600px; } .button { font-size: 36px; width: 150px; color: #41B883; text-align: center; padding-top: 25px; padding-bottom: 25px; border-width: 2px; border-style: solid; margin-right: 20px; border-color: rgb(162, 217, 192); background-color: rgba(162, 217, 192, 0.2); } </style> <script> var websocket = weex.requireModule('webSocket') export default { data () { return { connectinfo: '', sendinfo: '', onopeninfo: '', onmessage: '', oncloseinfo: '', onerrorinfo: '', closeinfo: '', txtInput:'', navBarHeight: 88, title: 'Navigator', dir: 'examples', baseURL: '' } }, methods: { connect:function() { websocket.WebSocket('ws://echo.websocket.org',''); var self = this; self.onopeninfo = 'connecting...' websocket.onopen = function(e) { self.onopeninfo = 'websocket open'; } websocket.onmessage = function(e) { self.onmessage = e.data; } websocket.onerror = function(e) { self.onerrorinfo = e.data; } websocket.onclose = function(e) { self.onopeninfo = ''; self.onerrorinfo = e.code; } }, send:function(e) { var input = this.$refs.input; input.blur(); websocket.send(this.txtInput); this.sendinfo = this.txtInput; }, oninput: function(event) { this.txtInput = event.value; }, close:function(e) { websocket.close(); }, }, } </script>
|