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
| <script> new Vue({ el: '#app', data: { value: 'haha' }, methods: { init() { if(window.WebSocket){ var ws = new WebSocket('ws://localhost:4444');
ws.onopen = function(e){ console.log("连接服务器成功"); ws.send("what`s your name?"); } ws.onclose = function(e){ console.log("服务器关闭"); } ws.onerror = function(){ console.log("连接出错"); } ws.onmessage = function(e){ let message = "message:"+e.data+""; console.log(message); } } } }, mounted () { this.init() } }) </script>
|