지금 아래는 자바스크립트부분이구요
$(document).ready(function(){
if(!("WebSocket" in window)){
$('#chatLog, input, button, #examples').fadeOut("fast");
$('<p>Oh no, you need a browser that supports WebSockets.</p>').appendTo('#container');
}else{
connect();
}
});
function connect(){
try{
var socket;
var socket = new WebSocket("ws://192.168.0.2:1000");
message('<p class="event">Socket Status: '+socket.readyState);
socket.onopen = function(){
message('<p class="event">Socket Status: '+socket.readyState+'(open)');
}
socket.onmessage = function(msg){
message('<p class="message">Received: '+msg.data);
}
socket.onclose = function(){
message('<p class="event">Socket Status: '+socket.readyState+' (Closed)');
}
}catch(exception){message('<p>Error'+exception);}
function message(msg){
$('#chatLog').append(msg+'</p>');
}
function send(){
var text = $('#text').val();
if(text==""){
message('<p class="warning">Please enter a message');
return ;
}
try{
this.send = function (message, callback) {
this.waitForConnection(function () {
ws.send(message);
if (typeof callback !== 'undefined') {
callback();
}
}, 1000);
};
this.waitForConnection = function (callback, interval) {
if (ws.readyState === 1) {
callback();
} else {
var that = this;
// optional: implement backoff for interval here
setTimeout(function () {
that.waitForConnection(callback, interval);
}, interval);
}
};
socket.send(text);
message('<p class="event">Send: '+text)
} catch(exception){
message('<p class="warning"> Error:' + exception);
}
$('#text').val("");
}
$('#disconnect').click(function(){
socket.close();
});
$('#text').keypress(function(event){
if(event.keyCode == '13'){send();}
});
}
이제 아래는 html 부분입니다
<div id="wrapper">
<div id="container">
<h1>WebSockets Client</h1>
<div id="chatLog">
</div>
<p id="examples">e.g. try 'hi', 'name', 'age', 'today'</p>
<input id="text" type="text" />
<button id="disconnect">Disconnect</button><br>
<input type="text" id="inputMessage" value="Hello, Web Socket!"><button id="sendButton">Send</button>
<pre id="output"></pre>
</div>
</div>
head에 스크립트로 jquery 넣었구요 보낼 내용 입력하고 엔터누르면 Error:InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 이렇게 뜨네요.. 연결되는 컴에 보면 log 같은것들이 다 뜨고 그 뒤에 아이피가 연결됐다고 뜨네요 http://html5korea.com/%EC%9B%B9%EC%86%8C%EC%BC%93websocket-%EC%8B%9C%EC%9E%91%ED%95%98%EA%B8%B0%EA%B0%95%EC%9D%98%EB%B2%88%EC%97%AD/ 여기보고한건데 제가 잘못된건가요?
+ 서버에서 메시지를 보내면 Closed 되버립니다..왜이런걸까요