我正在尝试将 PHP 函数转换为 jQuery 函数,以便我可以直接从浏览器使用 TCP/IP 套接字本地连接。
我用这个:
$socket = @fsockopen('xxx.xxx.xxx.xxx', '8810', $err_no, $err_str);
if(!$socket){
return 'Errore #'.$err_no.': '.$err_str;
}else{
fwrite($socket, '["D";"C";"?ST";200]');
$read = fread($socket, 2024);
//other stuff...
return $read;
fclose($socket);
}
这很好用。然后我下载了Github jQuery Websocket 0.0.4来自 Google 代码网站并按照示例进行操作,但没有成功。
我只是尝试以这种方式建立连接并发送数据:
ws = $.websocket("ws://95.110.224.199:8810/");
ws.send('string', '["D";"C";"?ST";200]');
这给了我“未定义不是函数”错误。
然后我尝试通过这种方式查看连接是否真正建立(不发送数据):
var ws = $.websocket("ws://xxx.xxx.xxx.xxx:8810/", {
open: function() {console.log('WS:connesso')},
close: function() {console.log('WS:chiuso')},
});
我运气不好...控制台什么也没说... 对此有任何提示或帮助吗?如有任何帮助,我们将不胜感激。
最佳答案
也许您错过了 jquery.websocket.js
文件,但您可以使用 JavaScript Web 套接字:
ws = new WebSocket('ws://95.110.224.199:8810/');
或者你可以输入 0.0.0.0 代替你的 ip,因为有些服务器不允许直接 ip 访问:
ws = new WebSocket('ws://0.0.0.0:8810/')
ws.onopen = function(msg) {
// Logic for opened connection
console.log('Connection successfully opened');
};
ws.onmessage = function(msg) {
// Handle received data
};
ws.onclose = function(msg) {
// Logic for closed connection
console.log('Connection was closed.');
}
ws.error =function(err){
console.log(err); // Write errors to console
}
关于javascript - jQuery Web Socket 未连接且未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24483927/