jSocketJavaScript的Socket通讯包

联合创作 · 2023-09-22 17:27

jSocket 是 JavaScript 实现 Socket 通讯的解决方案。

示例代码:

<script type='text/javascript'>
       
// Host we are connecting to
       
var host = 'localhost';
       
// Port we are connecting on
       
var port = 3000;
       
       
var socket = new jSocket();
       
       
// When the socket is added the to document
        socket
.onReady = function(){
                socket
.connect(host,port);              
       
}
       
       
// Connection attempt finished
        socket
.onConnect = function(success){
               
if(success){
                       
// Send something to the socket
                        socket
.send('Hello world');            
               
}else{
                        alert
('Connection to the server could not be estabilished');            
               
}      
       
}
        socket
.onData = function(data){
                alert
('Received from socket: '+data);  
       
}
       
       
// Setup our socket in the div with the id="socket"
        socket
.setup('#socket');        
 
</script>
浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报