drpmazn9021 2015-04-17 09:11
浏览 67
已采纳

如何从Web套接字开始?

I want to create a basic chat application using web socket in JavaScript. I searched a lot about it on various search engines but I didn't found anything that could help me.

I am using Wamp server and don't have NODE.js installed.

  1. Can I create an application on Wamp?
  2. How to write a server code for Web Sockets?

Note: I am aware of Client code for web socket but don't from where to start with server code? The code I need can be in PHP or JavaScript. Below is a basic client code written by Me.

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
  if ("WebSocket" in window)
{
  alert("WebSocket is supported by your Browser!");
 // Let us open a web socket
 var ws = new WebSocket("ws://localhost:9998/echo");
 ws.onopen = function()
 {
    // Web Socket is connected, send data using send()
    ws.send("Message to send");
    alert("Message is sent...");
 };
 ws.onmessage = function (evt) 
 { 
    var received_msg = evt.data;
    alert("Message is received...");
 };
 ws.onclose = function()
 { 
    // websocket is closed.
    alert("Connection is closed..."); 
 };
}
else
{
   // The browser doesn't support WebSocket
    alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
   <a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongxu1875 2015-04-17 09:24
    关注

    If you are looking into a basic chat application you do not need web sockets, an AJAX polling will suffice.

    The problem with PHP web sockets is that to use them you will need to create your own PHP server that will open a socket and listen on it. To do so you need to be able to run PHP scripts as CLI - you will need to have shell access on your server, which is not available on most shared hosting providers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题