How to update value of var in HTML? - javascript

I have simple project about connect between java server and javascript client via websocket.
Here my HTML file:
<body>
<h3>Demo WebSocket</h3>
<script type="text/javascript">
var arr;
var websocket;
connect();
function connect() {
websocket = new WebSocket('ws://127.0.0.1:4444');
websocket.onopen = function () {
var i = (typeof websocket != 'undefined' && websocket.readyState == WebSocket.OPEN);
textAreaMessage.value += "Server ..." + i + "\n";
};
websocket.onmessage = function (message) { processMessage(message); };
websocket.onclose = function (message) { processClose(message); };
websocket.onerror = function (message) { processError(message); };
}
function disconnect() {
websocket.close();
}
function processOpen(message) {
}
function processMessage(message) {
console.log(message);
arr = JSON.parse(message.data)
textAreaMessage.value += "Response From Server ==> " + arr[0] + " \n";
}
function processClose(message) {
textAreaMessage.value += "Server Disconnect... \n";
}
function processError(message) {
textAreaMessage.value += "Error... " + message + " \n";
}
function sendMessage() {
if (typeof websocket != 'undefined' && websocket.readyState == WebSocket.OPEN) {
websocket.send(textMessage.value);
textAreaMessage.value += "Send to Server ==> " + textMessage.value + " \n";
textMessage.value = "";
}
}
</script>
</body>
when server send String via websocket to client, value of massage will store in mesage.data but i converted it to JavaScript object use arr = JSON.parse(message.data)
And then i conole.log(arr) and i see:
and I get each element of arr via index like: arr[index].
The important thing is when the new value come via websocket then the value of element also change. I want display the value of element in page and its displayed value automatically changes when its value is changed.
Somebody can help me?
Sorry about my grammar.
Thank you so much!

Related

TryCatch with Websocket on two IPs is not handled in javascript

I have a websocket listening on some interfaces so the client could call more than one ip. I don't have a dns for these IPs. This failes even in the try block
window.onload = function() {
ws = "";
try {
ws_connection = "ws://" + lblInfoIP.value + ":9080/websockets";
ws = new WebSocket(ws_connection);
}
catch(err) {
ws_connection = "ws://127.0.01:9080/websockets";
ws = new WebSocket(ws_connection);
}
ws.onmessage = function(msg) { showInfo(msg.data); };
ws.onerror = function(evt){ alert ('Websocket failed with ' + evt.data) };
}
manually using the same ip as the called url works.
How would I correctly handle that ?
Is there somesthing like ws_connection = "ws://" + called_url + "/websockets"; ?
Instead of your try/catch construct, use a simple "or" (||) operation:
window.onload = function () {
let ws = new WebSocket(`ws://${lblInfoIP.value || "127.0.01:9080"}/websockets`)
ws.onmessage = function (msg) { showInfo(msg.data); };
ws.onerror = function (evt) { alert('Websocket failed with ' + evt.data) };
}
In development, set lblInfoIP.value to null or "undefined".
Or use "document.location.host:9080"

Telegram API returning HTML instead of JSON

I'm writing a telegram bot to report fail2ban bans. It's very simple and dirty, written hastily, but it can be used to report any message to a single telegram user:
var TelegramBot = require('node-telegram-bot-api');
var fs = require('fs');
var store = {
get: function (key) {
return fs.readFileSync(__dirname + '/' + key, { encoding: 'utf-8' });
},
set: function (key, value) {
fs.writeFileSync(__dirname + '/' + key, value, { encoding: 'utf-8' });
}
};
var token = store.get('token');
var args = process.argv.slice(2);
if (args.length == 0) {
console.error('No mode specified');
process.exit(0);
}
TelegramBot.prototype.unregisterText = function (regexp) {
for (var i = 0; i < bot.textRegexpCallbacks.length; ++i) {
if (bot.textRegexpCallbacks[i].regexp.toString() == regexp) {
bot.textRegexpCallbacks.splice(i, 1);
return;
}
}
};
fs.appendFileSync(__dirname + '/logs',
'[' + (new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')) + '] '
+ args.join(' ') + '\n',
{ encoding: 'utf-8' });
switch (args[0]) {
case 'setup':
var bot = new TelegramBot(token, { polling: true });
var step = 'none';
bot.onText(/\/setup/, function (msg, match) {
var fromId = msg.from.id;
step = 'setup-started';
bot.sendMessage(fromId, 'Starting setup. Please enter the verification key.');
bot.onText(/(.+)/, function (msg, match) {
if (step == 'setup-started') {
var key = match[1];
var verification = store.get('key');
if (key == verification) {
store.set('owner', msg.from.id);
step = 'verified';
bot.sendMessage(msg.from.id, 'Correct. Setup complete.');
} else {
step = 'none';
bot.unregisterText(/(.+)/);
bot.sendMessage(msg.from.id, 'Wrong. Setup aborted.');
}
}
});
});
break;
case 'report':
var bot = new TelegramBot(token, { polling: false });
var owner = store.get('owner');
var subject = args[1];
if (subject == 'message') {
var message = args.slice(2).join(' ');
bot.sendMessage(owner, message);
} else if (subject == 'file') {
var content = fs.readFileSync(args[2], { encoding: 'utf-8' });
bot.sendMessage(owner, content);
}
break;
default:
console.error('Unrecognized mode', args[0]);
break;
}
On my developer machine it works fine. I invoke:
node bot.js report message whatever message i want
And I correctly received "whatever message i want" on telegram. However, once I gitted it on my digitalocean vps, it no longer worked. It turns out the problem is with the telegram library:
Unhandled rejection Error: Error parsing Telegram response: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bots: An introduction for developers</title>
...
Which apparently returns an html page instead of json... I also tried to contact the same endpoint (api.telegram.org/bothash/sendMessage) with curl on my vps and it returned json (with an error message because i didnt send any parameters, but still json).
I cannot fathom why this happens. Any help?
It seems like either you don't have a file with token on your VPN or the token is incorrect.
You can check it by yourself:
When you make a request to api.telegram.org/{token}/sendMessage, and {token} is incorrect, it redirects you to this page, which responds with HTML you've mentioned in your question.
So you have to debug a behavior of your store.get and store.get functions along with files and tokens to make sure you are using a correct one.
Also, I'd recommend to run bot.getMe() before using any other Telegram API methods to ensure you specified a correct bot token.

Uncaught type error: Cannot read property of 'send' undefined

I am trying to send a message to my websocket in the form of json but I got this error in the developer console when I run the web app. I know this error occurs when the element could not be found. However, isn't 'send' a default member of websocket? Why wouldn't it be found? The error occurs at my last line of code.The code for my .js file is as follows:
// to keep the session id
var sessionId = '';
// name of the client
var name = '';
$(document).ready(function() {
$("#form_submit, #form_send_message").submit(function(e) {
e.preventDefault();
join();
});
});
var webSocket;
/**
* Connecting to socket
*/
function join() {
// Checking person name
if ($('#input_name').val().trim().length <= 0) {
alert('Enter your name');
} else {
name = $('#input_name').val().trim();
$('#prompt_name_container').fadeOut(1000, function() {
// opening socket connection
openSocket();
});
}
return false;
}
/**
* Will open the socket connection
*/
function openSocket() {
// Ensures only one connection is open at a time
if (webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED) {
return;
}
var wsUrl;
if (window.location.protocol == 'http:') {
wsUrl = 'ws://' + window.location.host + ':8000/chat'+'?name='+name;
} else {
wsUrl = 'wss://' + window.location.host + ':8443/chat'+'?name='+name;
}
console.log('WebSockets Url : ' + wsUrl);
var webSocket = new WebSocket(wsUrl);
// Create a new instance of the websocket
//webSocket = new WebSocket("ws://jbosslew-weihao.rhcloud.com:8000/" );
/**
* Binds functions to the listeners for the websocket.
*/
webSocket.onopen = function(event) {
$('#message_container').fadeIn();
if (event.data === undefined)
return;
};
webSocket.onmessage = function(event) {
// parsing the json data
parseMessage(event.data);
};
webSocket.onclose = function(event) {
};
}
/**
* Sending the chat message to server
*/
function send() {
var message = $('#input_message').val();
if (message.trim().length > 0) {
sendMessageToServer('message', message);
} else {
alert('Please enter message to send!');
}
}
/**
* Closing the socket connection
*/
function closeSocket() {
webSocket.close();
$('#message_container').fadeOut(600, function() {
$('#prompt_name_container').fadeIn();
// clearing the name and session id
sessionId = '';
name = '';
// clear the ul li messages
$('#messages').html('');
$('p.online_count').hide();
});
}
/**
* Parsing the json message. The type of message is identified by 'flag' node
* value flag can be self, new, message, exit
*/
function parseMessage(message) {
var jObj = $.parseJSON(message);
// if the flag is 'self' message contains the session id
if (jObj.flag == 'self') {
sessionId = jObj.sessionId;
} else if (jObj.flag == 'new') {
// if the flag is 'new', a client joined the chat room
var new_name = 'You';
// number of people online
var online_count = jObj.onlineCount;
$('p.online_count').html(
'Hello, <span class="green">' + name + '</span>. <b>'
+ online_count + '</b> people online right now')
.fadeIn();
if (jObj.sessionId != sessionId) {
new_name = jObj.name;
}
var li = '<li class="new"><span class="name">' + new_name + '</span> '
+ jObj.message + '</li>';
$('#messages').append(li);
$('#input_message').val('');
} else if (jObj.flag == 'message') {
// if the json flag is 'message', it means somebody sent the chat
// message
var from_name = 'You';
if (jObj.sessionId != sessionId) {
from_name = jObj.name;
}
var li = '<li><span class="name">' + from_name + '</span> '
+ jObj.message + '</li>';
// appending the chat message to list
appendChatMessage(li);
$('#input_message').val('');
} else if (jObj.flag == 'exit') {
// if the json flag is 'exit', it means somebody left the chat room
var li = '<li class="exit"><span class="name red">' + jObj.name
+ '</span> ' + jObj.message + '</li>';
var online_count = jObj.onlineCount;
$('p.online_count').html(
'Hello, <span class="green">' + name + '</span>. <b>'
+ online_count + '</b> people online right now');
appendChatMessage(li);
}
}
/**
* Appending the chat message to list
*/
function appendChatMessage(li) {
$('#messages').append(li);
// scrolling the list to bottom so that new message will be visible
$('#messages').scrollTop($('#messages').height());
}
/**
* Sending message to socket server message will be in json format
*/
function sendMessageToServer(flag, message) {
var json = '{""}';
// preparing json object
var myObject = new Object();
myObject.sessionId = sessionId;
myObject.message = message;
myObject.flag = flag;
// converting json object to json string
json = JSON.stringify(myObject);
// sending message to server
webSocket.send(json); // Uncaught type error occurs here
}
If it is also relevant, my java code for my server socket is as follows:
package info.androidhive.webmobilegroupchat;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.common.collect.Maps;
#ServerEndpoint("/chat")
public class SocketServer {
// set to store all the live sessions
private static final Set<Session> sessions = Collections
.synchronizedSet(new HashSet<Session>());
// Mapping between session and person name
private static final HashMap<String, String> nameSessionPair = new HashMap<String, String>();
private JSONUtils jsonUtils = new JSONUtils();
// Getting query params
public static Map<String, String> getQueryMap(String query) {
Map<String, String> map = Maps.newHashMap();
if (query != null) {
String[] params = query.split("&");
for (String param : params) {
String[] nameval = param.split("=");
map.put(nameval[0], nameval[1]);
}
}
return map;
}
/**
* Called when a socket connection opened
* */
#OnOpen
public void onOpen(Session session) {
System.out.println(session.getId() + " has opened a connection");
Map<String, String> queryParams = getQueryMap(session.getQueryString());
String name = "";
if (queryParams.containsKey("name")) {
// Getting client name via query param
name = queryParams.get("name");
try {
name = URLDecoder.decode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Mapping client name and session id
nameSessionPair.put(session.getId(), name);
}
// Adding session to session list
sessions.add(session);
try {
// Sending session id to the client that just connected
session.getBasicRemote().sendText(
jsonUtils.getClientDetailsJson(session.getId(),
"Your session details"));
} catch (IOException e) {
e.printStackTrace();
}
// Notifying all the clients about new person joined
sendMessageToAll(session.getId(), name, " joined conversation!", true,
false);
}
/**
* method called when new message received from any client
*
* #param message
* JSON message from client
* */
#OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
String msg = null;
// Parsing the json and getting message
try {
JSONObject jObj = new JSONObject(message);
msg = jObj.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
// Sending the message to all clients
sendMessageToAll(session.getId(), nameSessionPair.get(session.getId()),
msg, false, false);
}
/**
* Method called when a connection is closed
* */
#OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
// Getting the client name that exited
String name = nameSessionPair.get(session.getId());
// removing the session from sessions list
sessions.remove(session);
// Notifying all the clients about person exit
sendMessageToAll(session.getId(), name, " left conversation!", false,
true);
}
/**
* Method to send message to all clients
*
* #param sessionId
* #param message
* message to be sent to clients
* #param isNewClient
* flag to identify that message is about new person
* #param isExit
* flag to identify that a person left the conversation
* */
private void sendMessageToAll(String sessionId, String name,
String message, boolean isNewClient, boolean isExit) {
// Looping through all the sessions and sending the message individually
for (Session s : sessions) {
String json = null;
// Checking if the message is about new client joined
if (isNewClient) {
json = jsonUtils.getNewClientJson(sessionId, name, message,
sessions.size());
} else if (isExit) {
// Checking if the person left the conversation
json = jsonUtils.getClientExitJson(sessionId, name, message,
sessions.size());
} else {
// Normal chat conversation message
json = jsonUtils
.getSendAllMessageJson(sessionId, name, message);
}
try {
System.out.println("Sending Message To: " + sessionId + ", "
+ json);
s.getBasicRemote().sendText(json);
} catch (IOException e) {
System.out.println("error in sending. " + s.getId() + ", "
+ e.getMessage());
e.printStackTrace();
}
}
}
}
According to Tiny Giant, I've accidentally redeclared my variable webSocket, instead of using the global version of it, causing my webSocket to be undefined at that moment.

Send Message to Socket, from C# client to node.js + socket.io server

I would like to send a message through socket with c# windows store app client to node.js and socket.io server
my client side code is like this (c#)
private async void SendDatatoSocket(string sendTextData)
{
if (!connected)
{
StatusText = "Must be connected to send!";
return;
}
Int32 wordlength = 0; // Gets the UTF-8 string length.
try
{
OutputView = "";
StatusText = "Trying to send data ...";
txtblock_showstatus.Text += System.Environment.NewLine;
txtblock_showstatus.Text += StatusText;
Debug.WriteLine(StatusText);
// add a newline to the text to send
string sendData = sendTextData + Environment.NewLine;
DataWriter writer = new DataWriter(clientSocket.OutputStream);
wordlength = sendData.Length; // Gets the UTF-8 string length.
// Call StoreAsync method to store the data to a backing stream
await writer.StoreAsync();
StatusText = "Data was sent" + Environment.NewLine;
txtblock_showstatus.Text += System.Environment.NewLine;
txtblock_showstatus.Text += StatusText;
Debug.WriteLine(StatusText);
// detach the stream and close it
writer.DetachStream();
writer.Dispose();
}
catch (Exception exception)
{
// If this is an unknown status,
// it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
StatusText = "Send data or receive failed with error: " + exception.Message;
txtblock_showstatus.Text += System.Environment.NewLine;
txtblock_showstatus.Text += StatusText;
Debug.WriteLine(StatusText);
// Could retry the connection, but for this simple example
// just close the socket.
closing = true;
clientSocket.Dispose();
clientSocket = null;
connected = false;
}
// Now try to receive data from server
try
{
OutputView = "";
StatusText = "Trying to receive data ...";
Debug.WriteLine(StatusText);
txtblock_showstatus.Text += System.Environment.NewLine;
txtblock_showstatus.Text += StatusText;
DataReader reader = new DataReader(clientSocket.InputStream);
string receivedData;
reader.InputStreamOptions = InputStreamOptions.Partial;
var count = await reader.LoadAsync(512);
if (count > 0)
{
receivedData = reader.ReadString(count);
Debug.WriteLine(receivedData);
txtblock_showstatus.Text += System.Environment.NewLine;
txtblock_showstatus.Text += receivedData;
}
}
catch (Exception exception)
{
// If this is an unknown status,
// it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
StatusText = "Receive failed with error: " + exception.Message;
Debug.WriteLine(StatusText);
// Could retry, but for this simple example
// just close the socket.
closing = true;
clientSocket.Dispose();
clientSocket = null;
connected = false;
}
}
and my code in server side is like this (node.js)
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 1337;
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function (sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sock.on('data', function (data) {
console.log(sock.name + "> " + data, sock);
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function (data) {
console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
sock.end();
});
}).listen(PORT, HOST);
before, I changed the node.js code to
net.createServer(function (sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sock.write("Hello");
//});
The message "Hello" appear on my client side correctly
the problem is that when I add these lines, the code doesn't work anymore.
sock.on('data', function (data) {
console.log(sock.name + "> " + data, sock);
});
The message I sent is just a word of string.
It seems like the message doesn't go right in this.
sock.on('data', function (data) {} );
Is there anyway that I can make this thing work?
Thank you.
this is application Server side (Node Js):
var net = require('net');
var server = net.createServer(function(socket) { //Create the server and pass it the function which will write our data
console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);
socket.write("Hello\n");
socket.write("World!\n");
//when to open the C # application write in the Desktop console "hello world"
socket.on('data', function (data) {
console.log(socket.name + "> " + data);
socket.write("Message from server to Desktop");
socket.end("End of communications.");
});
});
server.listen(3000); //This is the port number we're listening to
Into my C# Application I have write:
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect("192.168.x.x", 3000); //Connect to the server on our local host IP address, listening to port 3000
NetworkStream clientStream = client.GetStream();
System.Threading.Thread.Sleep(1000); //Sleep before we get the data for 1 second
while (clientStream.DataAvailable)
{
byte[] inMessage = new byte[4096];
int bytesRead = 0;
try
{
bytesRead = clientStream.Read(inMessage, 0, 4096);
}
catch { /*Catch exceptions and handle them here*/ }
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine(encoder.GetString(inMessage, 0, bytesRead));
}
//******************** SEND DATA **********************************
string message = "Send message from Desktop to Server NodeJs!";
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Call StoreAsync method to store the data to a backing stream
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
//*****************************************************************
client.Close();
System.Threading.Thread.Sleep(10000); //Sleep for 10 seconds
}
this is my my working solution

WebSockets using Fleck. WSS

I try to create simple chat application using secure layer WSS. Without wss it works. Here is my code:
FleckLog.Level = LogLevel.Info;
var allsockets = new List<IWebSocketConnection>();
var server = new WebSocketServer("wss://localhost:8181");
server.Certificate = new X509Certificate2(#"C:\Users\user\Desktop\sharpchat-master\server\Sharpchat\Certificate.pfx", "123");
server.Start(socket =>
{
socket.OnOpen = () =>
{ //See socket.ConnectionInfo.* for additional informations
Console.WriteLine(String.Empty);
Console.WriteLine("[NEW CLIENT CONNECTION]======================");
Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
Console.WriteLine("=============================================");
Console.WriteLine(String.Empty);
allsockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine(String.Empty);
Console.WriteLine("[DISCONNECTED CLIENT]=======================");
Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
Console.WriteLine("=============================================");
Console.WriteLine(String.Empty);
allsockets.Remove(socket);
};
socket.OnMessage = (message) =>
{
//TODO: Json.Net Deserialize
Console.WriteLine("[JSON MESSAGE] " + message);
allsockets.ToList().ForEach(s => s.Send(message));
};
});
var input = Console.ReadLine();
while (input != "exit")
{
foreach (var socket in allsockets.ToList())
{
socket.Send(input);
}
input = Console.ReadLine();
}
When client connects to server there is an exception:
[Warn] Failed to Authenticate System.AggregateEx
ception: One or more errors occurred. ---> System.IO.IOException: The handshake
failed due to an unexpected packet format.
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncRes
ult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncRe
sult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar,
Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchron
ization)
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.IO.IOException: The handshake failed due to an
unexpected packet format.
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncRes
ult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncRe
sult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar,
Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchron
ization)<---
Here is client code in JavaScript:
// Websocket Endpoint url
var URL = 'wss://localhost:8181';
var chatClient = null;
function connect () {
chatClient = new WebSocket(URL);
chatClient.onmessage = function (event) {
var messagesArea = document.getElementById("messages");
var jsonObj = JSON.parse(event.data);
var message = "<"+ jsonObj.user + "> " + jsonObj.message + "\r\n";
messagesArea.value = messagesArea.value + message;
messagesArea.scrollTop = messagesArea.scrollHeight;
};
}
function disconnect () {
chatClient.close();
}
function sendMessage() {
var user = document.getElementById("userName").value.trim();
if (user === "")
alert ("Please enter your name!");
var inputElement = document.getElementById("messageInput");
var message = inputElement.value.trim();
if (message !== "") {
var jsonObj = {"user" : user, "message" : message};
chatClient.send(JSON.stringify(jsonObj));
inputElement.value = "";
}
inputElement.focus();
}
Can anyone help me fix this problem?
Thank you very much!
Use full domain name in URL i.e. var URL = 'wss://localhost.company.com:8181';
Allow invalid certificates for resources loaded from localhost # Enable.
More Details are here

Categories

Resources