Advertisement
Guest User

Untitled

a guest
May 24th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. var ws = null, ws_ready = 0;
  2.  
  3. $(function(){
  4. if ( wsc() ) {
  5. alert("Wrong Browser!");
  6. return -1;
  7. }
  8.  
  9. });
  10.  
  11. function login(){
  12. $.post("./cmd.php",
  13. $("#f_login").serializeArray(),
  14. function(data){
  15. if (data != "ok") {
  16. alert("login failed..");
  17. } else {
  18. window.location.href="./";
  19. }
  20. }
  21. );
  22. return false;
  23. }
  24.  
  25. function join(){
  26. $.post("./cmd.php",
  27. $("#f_join").serializeArray(),
  28. function(data){
  29. if (data != "ok") {
  30. alert("join failed..");
  31. } else {
  32. alert("join success");
  33. window.location.href="./";
  34. }
  35. }
  36. );
  37. return false;
  38. }
  39.  
  40. function transfer(){
  41. var data = {
  42. cmd : "transfer",
  43. id : $('#iid').val(),
  44. an : $('#an').val(),
  45. am : $('#am').val(),
  46. sk : $('#sk').val()
  47. };
  48. wss(data);
  49. return false;
  50. }
  51.  
  52. function logout(){
  53. $.post("./cmd.php",
  54. {cmd : "logout"},
  55. function(data){
  56. window.location.href="./";
  57. }
  58. );
  59. }
  60.  
  61. function wss(obj_data){
  62. if (ws_ready == 0) {
  63. setTimeout(function(){wss(obj_data);},500);
  64. return;
  65. }
  66. ret = ws.send(JSON.stringify(obj_data));
  67. //return ret;
  68. }
  69.  
  70. function wsc(){
  71. if ("WebSocket" in window)
  72. return false;
  73. return true;
  74. }
  75.  
  76. function global_sw(data){
  77. if (data == false){
  78. console.log("wrong connection");
  79. return -1;
  80. }
  81. result = JSON.parse(data);
  82. switch(result['c']){
  83. case "transfer" : {
  84. if (result['m']!="ok") {
  85. alert(result['m']);
  86. }
  87. }break;
  88.  
  89. case "list" : {
  90. listup(result['m']);
  91. }break;
  92.  
  93. default : console.log("unknown error");
  94. }
  95. }
  96. function list_init(){
  97. handleLoad();
  98. listing("balance", "desc");
  99. }
  100.  
  101. function list_sw(sw){
  102. if (sw==1) { oo = "user"; }
  103. else { oo = "balance"; }
  104. if (bb=="asc") {bb = "desc";}
  105. else {bb = "asc";}
  106. listing(oo, bb);
  107. }
  108.  
  109. function listing(c, d){
  110. oo = c; bb = d;
  111. wss({cmd:"list_init", o:c, b:d});
  112. }
  113.  
  114. function listup(data){
  115. list = JSON.parse(data);
  116. tbl = "<table width='100%' id='listtbl'>";
  117. tbl += "<thead><tr><td><a href='#' onclick='list_sw(1);'>USER</a></td>";
  118. tbl += "<td><a href='#' onclick='list_sw(2);'>BALANCE</a></td></tr></thead>";
  119. $.each(list, function(idx, data){
  120. tbl += "<tr><td>"+data['user']+"</td><td>"+data['balance']+"</td></tr>";
  121. });
  122. tbl += "</table>";
  123. $('.pageContent').html(tbl);
  124. }
  125.  
  126. function transfer_init(){
  127. handleLoad();
  128. }
  129.  
  130. function handleUnload(){
  131. ws.close();
  132. }
  133.  
  134. function handleLoad(){
  135. //ws = new WebSocket("ws://1.234.27.139:38089/banking");
  136. //ws = new WebSocket("ws://1.234.27.139:38090/banking");
  137. ws = new WebSocket("ws://1.234.27.139:40022/banking");
  138. ws.onopen = function(){
  139. console.log("opened.");
  140. ws_ready = 1;
  141. }
  142.  
  143. ws.onclose = function(){
  144. console.log("closed.");
  145. }
  146. ws.onmessage = function(evt){
  147. global_sw(evt.data);
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement