cncml手绘网
标题: 在IIS上搭建WebSocket服务器(三) [打印本页]
作者: admin 时间: 2018-11-7 00:42
标题: 在IIS上搭建WebSocket服务器(三)
编写客户端代码
1.新建一个*.html文件。
ws = new WebSocket('ws://192.168.85.128:8086/Handler1.ashx?user=' + $("#user").val());6 N5 H. G. A% d6 ^
这个地方的IP和端口号对应着我们搭建在IIS上的WebSocket服务器- <!DOCTYPE html>
" |! H( n; n$ R/ s+ q# q3 \, c$ @, d5 f - <html xmlns="http://www.w3.org/1999/xhtml">
! N* x' Z6 v& q0 {3 O- c: ? - <head>
9 S$ T5 f" h3 M+ O j+ T - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>, @1 ]: ^7 y/ h& `
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>7 u; v% x3 y- `" [+ ?
- <title></title>
) W& }7 P0 J& s4 }3 E4 `7 h - <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
# s8 {, J* h( n3 V. T - <script>
5 h( {# O$ R+ S3 j' o' h3 ~ - var ws;* c! Y- C! x9 w
- $().ready(function () {3 e$ y& x* x/ g* w2 S# M
- $('#conn').click(function () {6 s m" N) t" a7 k; m! X% n
- //ws = new WebSocket('ws://' + window.location.hostname + ':' + window.location.port + '/Handler1.ashx?user=' + $("#user").val());
/ S, C5 m% z: |, d' G0 m - ws = new WebSocket('ws://192.168.85.128:8086/Handler1.ashx?user=' + $("#user").val()); H3 O1 t% d: B2 U2 G% Z, f
- //var host = 'ws://192.168.85.128:8085/api/WSChat?user='+$("#user").val();
" F% O& L6 k: o# G$ q9 g; s - //var host = "ws://192.168.85.128:8085/api/WSChat";0 T5 b9 _! L5 a5 ^, W& M
- //webSocket = new WebSocket(host);1 j4 A7 G: i) C; w; T) _' _3 i' ?
-
# T" l: D' V- V1 |8 x - $('#msg').append('<p>正在连接</p>');
6 H, u8 z6 `; s2 e: x; v8 t
+ v. \) e, C7 P; J0 T( _ j% h4 L; {- ws.onopen = function () {
h3 a7 y, @$ L; U9 J6 D5 s - $('#msg').append('<p>已经连接</p>');
h* a' c( S: S$ D - }2 H/ p5 P' n' l# I- I0 e
- ws.onmessage = function (evt) {
) g3 ~$ V# C1 x; i - $('#msg').append('<p>' + evt.data + '</p>');
( ?9 N- ~, C' i! G9 c: n+ _) e/ \5 Y - }5 j) x. s: |+ d9 {6 M
- ws.onerror = function (evt) {: _ P2 k9 a- \- S1 o1 N+ a, y9 C. K
- $('#msg').append('<p>' + JSON.stringify(evt) + '</p>');# }' t# e; R/ [* @7 G8 G
- }
% ~' ^ S* Y/ v$ l. Z2 T$ R - ws.onclose = function () {
$ q4 Y9 \4 E* E6 k7 }0 o - $('#msg').append('<p>已经关闭</p>');
; }0 Z, I% G. ` - }8 d3 I( X5 z( [, a Z
- });
5 E" m, `3 _7 ~% i8 j3 L. L' y* ]
4 B6 `; j6 e" M" ?3 X1 O5 c- $('#close').click(function () {5 R2 M% r, N5 u$ p
- ws.close();. S+ _6 i) U$ t4 z0 {
- });5 V5 p$ S9 b* ^" M5 H
* c. W) g% ?1 O8 {/ H: {& Z- $('#send').click(function () {0 g# B. ]+ p+ K" \. [5 P! z7 Y- `
- if (ws.readyState == WebSocket.OPEN) {0 t% _- j- A8 q6 J
- ws.send($("#to").val() + "|" + $('#content').val());
: |9 a. L% }; b; h0 M - }
( [. G$ ]- c* x - else {: q4 q$ y6 ^# F' N
- $('#tips').text('连接已经关闭');
6 k/ L1 }1 w1 r0 _ - }
* A8 g7 L! H$ a, v) ~# t4 V6 U - });
8 |- x- s9 ]/ o m6 D3 H
8 x2 Q8 v3 c& I' c# T- });9 E8 _8 j0 v( N; @) |5 q! V% Y
- </script>
( [! Z+ Q$ ? v+ X3 ? - </head>
/ o6 Y- ?; h' I/ @ - <body>
& e8 i! v9 [: D( G9 | - <div>
3 m u7 ?7 e8 ~ - <input id="user" type="text" />& W9 s$ T/ {1 N* D f
- <input id="conn" type="button" value="连接" />
+ y9 f7 q, d0 E* T$ O - <input id="close" type="button" value="关闭"/><br />
- i4 t' B" V. H7 ]7 y. C - <span id="tips"></span>, [( |# X1 y) K v" O) P: f
- <input id="content" type="text" />$ J+ c) u2 v5 r' g* z: i% g; S8 m
- <input id="send" type="button" value="发送"/><br />- w* q' o$ W% S) }
- <input id="to" type="text" />目的用户/ @$ p/ n# h" l1 _3 j% [+ N6 q
- <div id="msg">
+ ^ b; s7 n5 O9 X+ b& n3 g' d - </div>1 M9 r, L- k& t
- </div>
- B( x- K6 @4 l9 x' K1 \$ D4 ?+ |% _ - </body>9 M( @: z v+ B3 Y: H* h% r3 y; ?
- </html>
复制代码2.客户端A和客户端B通信效果
在浏览器中分别打开两个窗口,左边为客户端A,右边为客户端B,点击“连接”按钮,AB客户端分别与服务器建立连接
填写要发送的内容,即可看到A和B互相发送的信息了,即实现了AB客户端实现了WebSocket即时通信。
1 `* J$ K5 q+ ]7 c
| 欢迎光临 cncml手绘网 (http://bbs.cncml.com/) |
Powered by Discuz! X3.2 |