ILOVEMYFAMILY_jiejie
ILOVEMYFAMILY_jiejie
2014-12-08 06:05
采纳率: 27.3%
浏览 2.5k

html5 websocket在线聊天

用html5 websocket 做的聊天室,怎么设置让客户端在知道服务器端的ip情况下,还是连不上呀!也就是说怎么对客户端进行身份验证?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • showbo
    已采纳

    楼主下的是http://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/这个地址的示例?如果是修改WebSocketsServer.cs这个文件里面的代码,增加客户端ip地址的判断,主要是获取客户端ip地址,然后判断ip是否允许连接

                while (true)
                {
                    Socket sc = Listener.Accept();
                    if (sc != null)
                    {
                        IPEndPoint cip = (IPEndPoint)sc.RemoteEndPoint;
                        string ipaddr = cip.Address.ToString();
    
                        if (ipaddr == "192.168.1.68")//这里改为你的其他判断,如读数据库中存在的ip地址对比什么的,在禁止ip列表里面就关闭连接
                            sc.Close();
                        else
                        {
                            System.Threading.Thread.Sleep(100);
                            SocketConnection socketConn = new SocketConnection();
                            socketConn.ConnectionSocket = sc;
                            socketConn.NewConnection += new NewConnectionEventHandler(socketConn_NewConnection);
                            socketConn.DataReceived += new DataReceivedEventHandler(socketConn_BroadcastMessage);
                            socketConn.Disconnected += new DisconnectedEventHandler(socketConn_Disconnected);
    
                            socketConn.ConnectionSocket.BeginReceive(socketConn.receivedDataBuffer,
                                                                     0, socketConn.receivedDataBuffer.Length,
                                                                     0, new AsyncCallback(socketConn.ManageHandshake),
                                                                     socketConn.ConnectionSocket.Available);
                            connectionSocketList.Add(socketConn);
    
                        }
                    }
                }
    
    点赞 评论
  • xuzuning
    xuzuning 2014-12-08 07:13

    身份验证是主动的,也就是应该有一个登录界面,提交成功后方可进入

    点赞 评论

相关推荐