爱问知识人 爱问教育 医院库

html客户端连接不上服务端

首页

html客户端连接不上服务端

大家好,我建的客户端提交数据后服务端显示不出来是怎么回事吗?不尽感谢!

提交回答

全部答案

    2017-10-21 05:14:04
  •   要做个简单的实时通讯对话, 
    网上找到了SuperSocket 快速做服务端应用,下面自己写的简单服务端
    C# code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 class Program ? ?{ ? ? ? ? static List _sessions = new List(); ? ? ? ? static void Main(string[] args) ? ? ? ?{ ? ? ? ? ? ?Console。
      WriteLine("Press any key to start the server!"); ? ? ? ? ? ?Console。ReadKey(); ? ? ? ? ? ?Console。WriteLine(); ? ? ? ? ? ? var appServer = new AppServer(); ? ? ? ? ? ? /* ? ? ? ? ? ? ? ?name: 服务器实例的名称; ? ? ? ? ? ? ? ?serverType: 服务器实例的类型的完整名称; ? ? ? ? ? ? ? ?serverTypeName: 所选用的服务器类型在 serverTypes 节点的名字,配置节点 serverTypes 用于定义所有可用的服务器类型,我们将在后面再做详细介绍; ? ? ? ? ? ? ? ?ip: 服务器监听的ip地址。
      你可以设置具体的地址,也可以设置为下面的值 Any - 所有的IPv4地址 IPv6Any - 所有的IPv6地址 ? ? ? ? ? ? ? ?port: 服务器监听的端口; ? ? ? ? ? ? ? ?listenBacklog: 监听队列的大小; ? ? ? ? ? ? ? ?mode: Socket服务器运行的模式, Tcp (默认) 或者 Udp; ? ? ? ? ? ? ? ?disabled: 服务器实例是否禁用了; ? ? ? ? ? ? ? ?startupOrder: 服务器实例启动顺序, bootstrap 将按照此值的顺序来启动多个服务器实例; ? ? ? ? ? ? ? ?sendTimeOut: 发送数据超时时间; ? ? ? ? ? ? ? ?sendingQueueSize: 发送队列最大长度, 默认值为5; ? ? ? ? ? ? ? ?maxConnectionNumber: 可允许连接的最大连接数; ? ? ? ? ? ? ? ?receiveBufferSize: 接收缓冲区大小; ? ? ? ? ? ? ? ?sendBufferSize: 发送缓冲区大小; ? ? ? ? ? ? ? ?syncSend: 是否启用同步发送模式, 默认值: false; ? ? ? ? ? ? ? ?logCommand: 是否记录命令执行的记录; ? ? ? ? ? ? ? ?logBasicSessionActivity: 是否记录session的基本活动,如连接和断开; ? ? ? ? ? ? ? ?clearIdleSession: true 或 false, 是否定时清空空闲会话,默认值是 false; ? ? ? ? ? ? ? ?clearIdleSessionInterval: 清空空闲会话的时间间隔, 默认值是120, 单位为秒; ? ? ? ? ? ? ? ?idleSessionTimeOut: 会话空闲超时时间; 当此会话空闲时间超过此值,同时clearIdleSession被配置成true时,此会话将会被关闭; 默认值为300,单位为秒; ? ? ? ? ? ? ? ?security: Empty, Tls, Ssl3。
       Socket服务器所采用的传输层加密协议,默认值为空; ? ? ? ? ? ? ? ?maxRequestLength: 最大允许的请求长度,默认值为1024; ? ? ? ? ? ? ? ?textEncoding: 文本的默认编码,默认值是 ASCII; ? ? ? ? ? ? ? ?defaultCulture: 此服务器实例的默认 thread culture, 只在。
      Net 4。5中可用而且在隔离级别为 'None' 时无效; ? ? ? ? ? ? ? ?disableSessionSnapshot: 是否禁用会话快照, 默认值为 false。 ? ? ? ? ? ? ? ?sessionSnapshotInterval: 会话快照时间间隔, 默认值是 5, 单位为秒; ? ? ? ? ? ? ? ?keepAliveTime: 网络连接正常情况下的keep alive数据的发送间隔, 默认值为 600, 单位为秒; ? ? ? ? ? ? ? ?keepAliveInterval: Keep alive失败之后, keep alive探测包的发送间隔,默认值为 60, 单位为秒; ? ? ? ? ? ? */ ? ? ? ? ? ?var serverConfig = new ServerConfig ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?Port = 4141, //set the listening port ? ? ? ? ? ? ? ?Ip = "Any", ? ? ? ? ? ? ? ?Name = "XHS_Server", ? ? ? ? ? ? ? ?//Other configuration options ? ? ? ? ? ? ? ?Mode = SocketMode。
      Tcp, ? ? ? ? ? ? ? ?MaxConnectionNumber = 100, ? ? ? ? ? ? ? ?IdleSessionTimeOut = 600, ? ? ? ? ? ? ? ?TextEncoding = "UTF-8", ? ? ? ? ? ? ? ?SyncSend = true, ? ? ? ? ? ? ? ?SendBufferSize = 1024, ? ? ? ? ? ? ? ?ReceiveBufferSize = 1024, ? ? ? ? ? ? ? ?LogBasicSessionActivity = true, ? ? ? ? ? ? ? ?LogAllSocketException = true, ? ? ? ? ? ? ? ?KeepAliveTime = 300 ? ? ? ? ? ?}; ? ? ? ? ? ?serverConfig。
      SyncSend = true; ? ? ? ? ? ? ? ? ? ? ? ? //Setup the appServer ? ? ? ? ? ?if (!appServer。Setup(serverConfig)) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?Console。
      WriteLine("Failed to setup!"); ? ? ? ? ? ? ? ?Console。ReadKey(); ? ? ? ? ? ? ? ?return; ? ? ? ? ? ?} ? ? ? ? ? ? ?appServer。
      NewSessionConnected = new SessionHandler(appServer_NewSessionConnected); ? ? ? ? ? ?appServer。NewRequestReceived = new RequestHandler(appServer_NewRequestReceived); ? ? ? ? ? ?appServer。
      SessionClosed = new SessionHandler(appServer_SessionClosed); ? ? ? ? ? ? Console。WriteLine(); ? ? ? ? ? ?//Try to start the appServer ? ? ? ? ? ?if (!appServer。
      Start()) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?Console。WriteLine("Failed to start!"); ? ? ? ? ? ? ? ?Console。ReadKey(); ? ? ? ? ? ? ? ?return; ? ? ? ? ? ?} ? ? ? ? ? ? Console。
      WriteLine("The server started successfully, press key 'q' to stop it!"); ? ? ? ? ? ? while (Console。ReadKey()。KeyChar != 'q') ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?Console。
      WriteLine(); ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ?} ? ? ? ? ? ? Console。WriteLine(); ? ? ? ? ? ?//Stop the appServer ? ? ? ? ? ?appServer。
      Stop(); ? ? ? ? ? ? Console。WriteLine("The server was stopped!"); ? ? ? ? } ? ? ? ? static void appServer_NewSessionConnected(AppSession session) ? ? ? ?{ ? ? ? ? ? ?_sessions。
      Add(session); ? ? ? ? ? ?session。Send("Welcome to SuperSocket Telnet Server"); ? ? ? ?} ? ? ? ? static void appServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo) ? ? ? ?{ ? ? ? ? ? ?string Key = requestInfo。
      Key; ? ? ? ? ? ?MyLogHelper。Instance。Debug("requestInfo。Key" Key); ? ? ? ? ? ?Logger。Log(string。Format("{0}{1}", Key, requestInfo。
      Body)); ? ? ? ? ? ?//session。Send(Key); ? ? ? ? ? ? foreach(var s in _sessions) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?s。Send(Key); ? ? ? ? ? ?} ? ? ? ?} ? ? ? ? static void appServer_SessionClosed(AppSession session,CloseReason closereason) ? ? ? ?{ ? ? ? ? ? ?Logger。
      Log("断开连接!"); ? ? ? ? ? ?session。Close(); ? ? ? ?} ? ? ? ? //public override void ExecuteCommand(AppSession session, StringRequestInfo requestInfo) ? ? ? ?//{ ? ? ? ?// ? ?session。
      Send(requestInfo。Body); ? ? ? ?//} ? ? ?}}
    用户windows命令直接telnet是可以链接上的,而且也可以发消息;但是用html5 的websocket就是连不上,提示:
    WebSocket connection to 'ws://192。
      168。108。104:4141/' failed: Error during WebSocket handshake: Invalid status line
    下面是我的html5代码:
    JavaScript code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/// $(document)。
      ready(function () { ? ? var WebSocketsExist = true; ? ?try { ? ? ? ?//new WebSocket("ws://" $("#Connection")。val()); ? ? ? ?var dummy = new WebSocket("ws://" $("#Connection")。
      val()); ? ?} catch (ex) { ? ? ? ?try { ? ? ? ? ? ?webSocket = new MozWebSocket("ws://" $("#Connection")。val()); ? ? ? ?} ? ? ? ?catch (ex) { ? ? ? ? ? ?WebSocketsExist = false; ? ? ? ?} ? ?} ? ? if (WebSocketsExist) { ? ? ? ?Log("您的浏览器支持WebSocket。
       您可以尝试连接到聊天服务器!"); ? ? ? ? ? ? } else { ? ? ? ?Log("您的浏览器不支持WebSocket。请选择其他的浏览器再尝试连接服务器。"); ? ? ? ? ? ? } ? ? }); var ws;var SocketCreated = false;function ConnectionSocket(){ ? ?Log("准备连接在线交流服务器 。
      。。"); ? ? try{ ? ? ? ?if ("WebSocket" in window) { ? ? ? ? ? ?ws = new WebSocket("ws://" $("#Connection")。val()); ? ? ? ?} ? ? ? ?else if ("MozWebSocket" in window) { ? ? ? ? ? ?ws = new MozWebSocket("ws://" $("#Connection")。
      val()); ? ? ? ?} ? ? ? ?SocketCreated = true; ? ?} ? ?catch (ex) { ? ? ? ?Log(ex); ? ? ? ?return; ? ?} ? ? ws。onopen = WSonOpen; ? ?ws。
      onmessage = WSonMessage; ? ?ws。onclose = WSonClose; ? ?ws。onerror = WSonError;} function connectjs() { ? ?ConnectionSocket();} //发送连接消息function WSonOpen() { ? ?Log("连接已经建立。
      "); ? ?ws。send("login:" $("#txtName")。val());}; //写消息function WSonMessage(event) { ? ?Log(event。data);}; //关闭function WSonClose() { ? ?Log("【" $("#txtName")。
      val() "】离开了!");}; //错误function WSonError(evt) { ? ? ? ?console。log(evt); ? ?Log("远程连接中断:" evt ""); ? ?SocketCreated = false; ? }; function sendjs(){ ? ?if ($("#talktxt")。
      val()。trim() != "") { ? ? ? ?ws。send("" $("#txtName")。val() " : [" Gettime() "]
    " $("#talktxt")。val() ""); ? ? ? ?$("#talktxt")。
      val(""); ? ?}; } ? function Log(text){ ? ?var st = "" text ""; ? ? $("#Ul_Talk")。append(st); ? ?$("#Ul_Talk")。scrollTop($("#Ul_Talk")[0]。
      scrollHeight);} function Gettime() { ? ?var myDate = new Date(); ? ?var Minu = myDate。getMinutes(); ? ?if (String(Minu)。length != 2) { ? ? ? ?Minu = "0" String(Minu); ? ?} ? ?var Second = myDate。
      getSeconds(); ? ?if (String(Second)。length != 2) { ? ? ? ?Second = "0" String(Second); ? ?} ? ? //myDate。getFullYear() "/" month "/" day " " ? ? var todayInfo = myDate。
      getHours() ":" Minu ":" myDate。getSeconds(); ? ? return todayInfo;

    我***

    2017-10-21 05:14:04

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):